1 #!/bin/bash
2
3 ### case
4 foo=a; case $foo in [0-9]) echo number;; [a-z]) echo letter;; esac
5 # stdout: letter
6
7 ### case in subshell
8 # Hm this subhell has to know about the closing ) and stuff like that.
9 # case_clause is a compound_command, which is a command. And a subshell
10 # takes a compound_list, which is a list of terms, which has and_ors in them
11 # ... which eventually boils down to a command.
12 echo $(foo=a; case $foo in [0-9]) echo number;; [a-z]) echo letter;; esac)
13 # stdout: letter
14
15 ### Command sub word part
16 # "The token shall not be delimited by the end of the substitution."
17 foo=FOO; echo $(echo $foo)bar$(echo $foo)
18 # stdout: FOObarFOO
19
20 ### Backtick
21 foo=FOO; echo `echo $foo`bar`echo $foo`
22 # stdout: FOObarFOO
23
24 ### Backtick 2
25 echo `echo -n l; echo -n s`
26 # stdout: ls
27
28 ### Nested backticks
29 # Inner `` are escaped! # Not sure how to do triple.. Seems like an unlikely
30 # use case. Not sure if I even want to support this!
31 echo X > $TMP/000000-first
32 echo `\`echo -n l; echo -n s\` $TMP | head -n 1`
33 # stdout: 000000-first
34
35 ### Making command out of command sub should work
36 # Works in bash and dash!
37 $(echo ec)$(echo ho) split builtin
38 # stdout: split builtin
39
40 ### Making keyword out of command sub should NOT work
41 # This doesn't work in bash or dash! Hm builtins are different than keywords /
42 # reserved words I guess.
43 # dash fails, but gives code 0
44 $(echo f)$(echo or) i in a b c; do echo $i; done
45 # status: 2
46 # BUG dash status: 0
47 # OK mksh status: 1
48
49 ### Command sub with here doc
50 echo $(<<EOF tac
51 one
52 two
53 EOF
54 )
55 # stdout: two one
56
57 ### Here doc with pipeline
58 <<EOF tac | tr '\n' 'X'
59 one
60 two
61 EOF
62 # stdout-json: "twoXoneX"
63
64 ### Command Sub word split
65 argv.py $(echo 'hi there') "$(echo 'hi there')"
66 # stdout: ['hi', 'there', 'hi there']
67
68 ### Command Sub trailing newline removed
69 s=$(python -c 'print "ab\ncd\n"')
70 argv "$s"
71 # stdout: ['ab\ncd']
72
73 ### Command Sub trailing whitespace not removed
74 s=$(python -c 'print "ab\ncd\n "')
75 argv "$s"
76 # stdout: ['ab\ncd\n ']