| 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 `\`echo -n l; echo -n s\` tests | head -n 1` |
| 32 | # stdout: 01-bad-func.sh |
| 33 | |
| 34 | ### Making command out of command sub should work |
| 35 | # Works in bash and dash! |
| 36 | $(echo ec)$(echo ho) split builtin |
| 37 | # stdout: split builtin |
| 38 | |
| 39 | ### Making keyword out of command sub should NOT work |
| 40 | # This doesn't work in bash or dash! Hm builtins are different than keywords / |
| 41 | # reserved words I guess. |
| 42 | # dash fails, but gives code 0 |
| 43 | $(echo f)$(echo or) i in a b c; do echo $i; done |
| 44 | # status: 2 |
| 45 | # BUG dash status: 0 |
| 46 | # OK mksh status: 1 |
| 47 | |
| 48 | ### Command sub with here doc |
| 49 | echo $(<<EOF tac |
| 50 | one |
| 51 | two |
| 52 | EOF |
| 53 | ) |
| 54 | # stdout: two one |
| 55 | |
| 56 | ### Here doc with pipeline |
| 57 | <<EOF tac | tr '\n' 'X' |
| 58 | one |
| 59 | two |
| 60 | EOF |
| 61 | # stdout-json: "twoXoneX" |