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 #### Storing backslashes and then echoing them
55 # This is a bug fix; it used to cause problems with unescaping.
56 one='\'
57 two='\\'
58 echo $one $two
59 echo "$one" "$two"
60 ## STDOUT:
61 \ \\
62 \ \\
63 ## END
64 ## BUG dash/mksh STDOUT:
65 \ \
66 \ \
67 ## END
68
69 #### Backslash escapes
70 echo \$ \| \a \b \c \d \\
71 ## stdout: $ | a b c d \
72
73 #### Backslash escapes inside double quoted string
74 echo "\$ \\ \\ \p \q"
75 ## stdout: $ \ \ \p \q
76
77 #### C-style backslash escapes inside double quoted string
78 # mksh and dash implement POSIX incompatible extensions. $ ` " \ <newline>
79 # are the only special ones
80 echo "\a \b"
81 ## stdout: \a \b
82 ## BUG dash/mksh stdout-json: "\u0007 \u0008\n"
83
84 # BUG
85
86 #### Literal $
87 echo $
88 ## stdout: $
89
90 #### Quoted Literal $
91 echo $ "$" $
92 ## stdout: $ $ $
93
94 #### Line continuation
95 echo foo\
96 $
97 ## stdout: foo$
98
99 #### Line continuation inside double quotes
100 echo "foo\
101 $"
102 ## stdout: foo$
103
104 #### $? split over multiple lines
105 # Same with $$, etc. OSH won't do this because $? is a single token.
106 echo $\
107 ?
108 ## stdout: $?
109 ## OK bash/mksh stdout: 0
110
111 #
112 # Bad quotes
113 #
114
115 # TODO: Also test unterminated quotes inside ${} and $()
116
117 #### Unterminated single quote
118 ## code: ls foo bar '
119 ## status: 2
120 ## OK mksh status: 1
121
122 #### Unterminated double quote
123 ## code: ls foo bar "
124 ## status: 2
125 ## OK mksh status: 1
126
127
128 #
129 # TODO: Might be another section?
130 #
131
132 #### Semicolon
133 echo separated; echo by semi-colon
134 ## stdout-json: "separated\nby semi-colon\n"
135
136 #
137 # TODO: Variable substitution operators.
138 #
139
140 #### No tab escapes within single quotes
141 # dash and mksh allow this, which is a BUG.
142 # POSIX says: "Enclosing characters in single-quotes ( '' ) shall preserve the
143 # literal value of each character within the single-quotes. A single-quote
144 # cannot occur within single-quotes"
145 echo 'a\tb'
146 ## stdout: a\tb
147 ## BUG dash/mksh stdout-json: "a\tb\n"
148
149 # See if it supports ANSI C escapes. Bash supports this, but dash does NOT. I
150 # guess dash you would do IFS=$(printf '\n\t')
151
152 #### $''
153 echo $'foo'
154 ## stdout: foo
155 ## N-I dash stdout: $foo
156
157 #### $'' with quotes
158 echo $'single \' double \"'
159 ## stdout: single ' double "
160 ## N-I dash stdout-json: ""
161 ## N-I dash status: 2
162
163 #### $'' with newlines
164 echo $'col1\ncol2\ncol3'
165 ## stdout-json: "col1\ncol2\ncol3\n"
166 # In dash, \n is special within single quotes
167 ## N-I dash stdout-json: "$col1\ncol2\ncol3\n"
168
169 #### $'' octal escapes don't have leading 0
170 # echo -e syntax is echo -e \0377
171 echo -n $'\001' $'\377' | od -A n -c | sed 's/ \+/ /g'
172 ## STDOUT:
173 001 377
174 ## END
175 ## N-I dash STDOUT:
176 $ 001 $ 377
177 ## END
178
179 #### $'' octal escapes with fewer than 3 chars
180 echo $'\1 \11 \11 \111' | od -A n -c | sed 's/ \+/ /g'
181 ## STDOUT:
182 001 \t \t I \n
183 ## END
184 ## N-I dash STDOUT:
185 $ 001 \t \t I \n
186 ## END
187
188 #### $""
189 echo $"foo"
190 ## stdout: foo
191 ## N-I dash/ash stdout: $foo
192
193 #### printf
194 # This accepts \t by itself, hm.
195 printf "c1\tc2\nc3\tc4\n"
196 ## stdout-json: "c1\tc2\nc3\tc4\n"