1 #!/usr/bin/env bash
2 #
3 # Tests for quotes.
4
5 ### Unquoted words
6 echo unquoted words
7 # stdout: unquoted words
8
9 ### Single-quoted
10 echo 'single quoted'
11 # stdout: single quoted
12
13 ### Two single-quoted parts
14 echo 'two single-quoted pa''rts in one token'
15 # stdout: two single-quoted parts in one token
16
17 ### Unquoted and single quoted
18 echo unquoted' and single-quoted'
19 # stdout: unquoted and single-quoted
20
21 ### newline inside single-quoted string
22 echo 'newline
23 inside single-quoted string'
24 # stdout-json: "newline\ninside single-quoted string\n"
25
26 ### Double-quoted
27 echo "double quoted"
28 # stdout: double quoted
29
30 ### Mix of quotes in one word
31 echo unquoted' single-quoted'" double-quoted "unquoted
32 # stdout: unquoted single-quoted double-quoted unquoted
33
34 ### Var substitution
35 FOO=bar
36 echo "==$FOO=="
37 # stdout: ==bar==
38
39 ### Var substitution with braces
40 FOO=bar
41 echo foo${FOO}
42 # stdout: foobar
43
44 ### Var substitution with braces, quoted
45 FOO=bar
46 echo "foo${FOO}"
47 # stdout: foobar
48
49 ### Var length
50 FOO=bar
51 echo "foo${#FOO}"
52 # stdout: foo3
53
54 ### Backslash escapes
55 echo \$ \| \a \b \c \d \\
56 # stdout: $ | a b c d \
57
58 ### Backslash escapes inside double quoted string
59 echo "\$ \\ \\ \p \q"
60 # stdout: $ \ \ \p \q
61
62 ### C-style backslash escapes inside double quoted string
63 # mksh and dash implement POSIX incompatible extensions. $ ` " \ <newline>
64 # are the only special ones
65 echo "\a \b"
66 # stdout: \a \b
67 # BUG dash/mksh stdout-json: "\u0007 \u0008\n"
68
69 # BUG
70
71 ### Literal $
72 echo $
73 # stdout: $
74
75 ### Quoted Literal $
76 echo $ "$" $
77 # stdout: $ $ $
78
79 ### Line continuation
80 echo foo\
81 $
82 # stdout: foo$
83
84 ### Line continuation inside double quotes
85 echo "foo\
86 $"
87 # stdout: foo$
88
89 ### $? split over multiple lines
90 # Same with $$, etc. OSH won't do this because $? is a single token.
91 echo $\
92 ?
93 # stdout: $?
94 # OK bash/mksh stdout: 0
95
96 #
97 # Bad quotes
98 #
99
100 # TODO: Also test unterminated quotes inside ${} and $()
101
102 ### Unterminated single quote
103 # code: ls foo bar '
104 # status: 2
105 # OK mksh status: 1
106
107 ### Unterminated double quote
108 # code: ls foo bar "
109 # status: 2
110 # OK mksh status: 1
111
112
113 #
114 # TODO: Might be another section?
115 #
116
117 ### Semicolon
118 echo separated; echo by semi-colon
119 # stdout-json: "separated\nby semi-colon\n"
120
121 #
122 # TODO: Variable substitution operators.
123 #
124
125 ### No tab escapes within single quotes
126 # dash and mksh allow this, which is a BUG.
127 # POSIX says: "Enclosing characters in single-quotes ( '' ) shall preserve the
128 # literal value of each character within the single-quotes. A single-quote
129 # cannot occur within single-quotes"
130 echo 'a\tb'
131 # stdout: a\tb
132 # BUG dash/mksh stdout-json: "a\tb\n"
133
134 # See if it supports ANSI C escapes. Bash supports this, but dash does NOT. I
135 # guess dash you would do IFS=$(printf '\n\t')
136
137 ### $''
138 echo $'foo'
139 # stdout: foo
140 # N-I dash stdout: $foo
141
142 ### $'' with newlines
143 echo $'col1\ncol2\ncol3'
144 # stdout-json: "col1\ncol2\ncol3\n"
145 # In dash, \n is special within single quotes
146 # N-I dash stdout-json: "$col1\ncol2\ncol3\n"
147
148 ### $""
149 echo $"foo"
150 # stdout: foo
151 # N-I dash stdout: $foo
152
153 ### printf
154 # This accepts \t by itself, hm.
155 printf "c1\tc2\nc3\tc4\n"
156 # stdout-json: "c1\tc2\nc3\tc4\n"