OILS / spec / quote.test.sh View on Github | oils.pub

308 lines, 94 significant
1## oils_failures_allowed: 2
2## compare_shells: dash bash mksh ash
3
4#### Unquoted words
5echo unquoted words
6## stdout: unquoted words
7
8#### Single-quoted
9echo 'single quoted'
10## stdout: single quoted
11
12#### Two single-quoted parts
13echo 'two single-quoted pa''rts in one token'
14## stdout: two single-quoted parts in one token
15
16#### Unquoted and single quoted
17echo unquoted' and single-quoted'
18## stdout: unquoted and single-quoted
19
20#### newline inside single-quoted string
21echo 'newline
22inside single-quoted string'
23## STDOUT:
24newline
25inside single-quoted string
26## END
27
28#### Double-quoted
29echo "double quoted"
30## stdout: double quoted
31
32#### Mix of quotes in one word
33echo unquoted' single-quoted'" double-quoted "unquoted
34## stdout: unquoted single-quoted double-quoted unquoted
35
36#### Var substitution
37FOO=bar
38echo "==$FOO=="
39## stdout: ==bar==
40
41#### Var substitution with braces
42FOO=bar
43echo foo${FOO}
44## stdout: foobar
45
46#### Var substitution with braces, quoted
47FOO=bar
48echo "foo${FOO}"
49## stdout: foobar
50
51#### Var length
52FOO=bar
53echo "foo${#FOO}"
54## stdout: foo3
55
56#### Storing backslashes and then echoing them
57# This is a bug fix; it used to cause problems with unescaping.
58one='\'
59two='\\'
60echo $one $two
61echo "$one" "$two"
62## STDOUT:
63\ \\
64\ \\
65## END
66## BUG dash/mksh STDOUT:
67\ \
68\ \
69## END
70
71#### Backslash escapes
72echo \$ \| \a \b \c \d \\
73## stdout: $ | a b c d \
74
75#### Backslash escapes inside double quoted string
76echo "\$ \\ \\ \p \q"
77## stdout: $ \ \ \p \q
78
79#### C-style backslash escapes inside double quoted string
80# mksh and dash implement POSIX incompatible extensions. $ ` " \ <newline>
81# are the only special ones
82echo "\a \b"
83## stdout: \a \b
84## BUG dash/mksh stdout-json: "\u0007 \u0008\n"
85
86# BUG
87
88#### Literal $
89echo $
90## stdout: $
91
92#### Quoted Literal $
93echo $ "$" $
94## stdout: $ $ $
95
96#### Line continuation
97echo foo\
98$
99## stdout: foo$
100
101#### Line continuation inside double quotes
102echo "foo\
103$"
104## stdout: foo$
105
106#### $? split over multiple lines
107# Same with $$, etc. OSH won't do this because $? is a single token.
108echo $\
109?
110## stdout: $?
111## OK dash/bash/mksh/ash stdout: 0
112
113#
114# Bad quotes
115#
116
117# TODO: Also test unterminated quotes inside ${} and $()
118
119#### Unterminated single quote
120## code: ls foo bar '
121## status: 2
122## OK mksh status: 1
123
124#### Unterminated double quote
125## code: ls foo bar "
126## status: 2
127## OK mksh status: 1
128
129
130#
131# TODO: Might be another section?
132#
133
134#### Semicolon
135echo separated; echo by semi-colon
136## STDOUT:
137separated
138by semi-colon
139## END
140
141#
142# TODO: Variable substitution operators.
143#
144
145#### No tab escapes within single quotes
146# dash and mksh allow this, which is a BUG.
147# POSIX says: "Enclosing characters in single-quotes ( '' ) shall preserve the
148# literal value of each character within the single-quotes. A single-quote
149# cannot occur within single-quotes"
150echo 'a\tb'
151## stdout: a\tb
152## BUG dash/mksh stdout-json: "a\tb\n"
153
154# See if it supports ANSI C escapes. Bash supports this, but dash does NOT. I
155# guess dash you would do IFS=$(printf '\n\t')
156
157#### $''
158echo $'foo'
159## stdout: foo
160## N-I dash stdout: $foo
161
162#### $'' with quotes
163echo $'single \' double \"'
164## stdout: single ' double "
165## N-I dash stdout-json: ""
166## N-I dash status: 2
167
168#### $'' with newlines
169echo $'col1\ncol2\ncol3'
170## STDOUT:
171col1
172col2
173col3
174## END
175# In dash, \n is special within single quotes
176## N-I dash STDOUT:
177$col1
178col2
179col3
180## END
181
182#### $'' octal escapes don't have leading 0
183# echo -e syntax is echo -e \0377
184echo -n $'\001' $'\377' | od -A n -c | sed 's/ \+/ /g'
185## STDOUT:
186 001 377
187## END
188## N-I dash STDOUT:
189 $ 001 $ 377
190## END
191
192#### $'' octal escapes with fewer than 3 chars
193echo $'\1 \11 \11 \111' | od -A n -c | sed 's/ \+/ /g'
194## STDOUT:
195 001 \t \t I \n
196## END
197## N-I dash STDOUT:
198 $ 001 \t \t I \n
199## END
200
201
202#### OSH allows invalid backslashes
203case $SH in dash|mksh) exit ;; esac
204
205w=$'\uZ'
206x=$'\u{03bc'
207y=$'\z'
208echo $w $x $y
209## STDOUT:
210\uZ \u{03bc \z
211## END
212## N-I dash/mksh stdout-json: ""
213
214#### YSH allows unquoted foo\ bar too
215shopt -s ysh:all
216touch foo\ bar
217ls foo\ bar
218## STDOUT:
219foo bar
220## END
221
222#### $"" is a synonym for ""
223echo $"foo"
224x=x
225echo $"foo $x"
226## STDOUT:
227foo
228foo x
229## END
230## N-I dash/ash STDOUT:
231$foo
232$foo x
233## END
234
235#### printf supports tabs
236# This accepts \t by itself, hm.
237printf "c1\tc2\nc3\tc4\n"
238## stdout-json: "c1\tc2\nc3\tc4\n"
239
240#### $'' supports \cA escape for Ctrl-A - mask with 0x1f
241
242# note: AT&T ksh supports this too
243
244case $SH in dash|ash) exit ;; esac
245
246show_bytes() {
247 # -A n - no file offset
248 od -A n -t c -t x1
249}
250
251# this isn't special
252# mksh doesn't like it
253#echo -n $'\c' | show_bytes
254
255echo -n $'\c0\c9-' | show_bytes
256echo
257
258# control chars are case insensitive
259echo -n $'\ca\cz' | show_bytes
260echo
261
262echo -n $'\cA\cZ' | show_bytes
263echo
264
265echo -n $'\c-\c+\c"' | show_bytes
266
267## STDOUT:
268 020 031 -
269 10 19 2d
270
271 001 032
272 01 1a
273
274 001 032
275 01 1a
276
277 \r \v 002
278 0d 0b 02
279## END
280## N-I dash/ash STDOUT:
281## END
282
283#### \c' is an escape, unlike bash
284
285# mksh and ksh agree this is an esacpe
286
287case $SH in dash|ash) exit ;; esac
288
289show_bytes() {
290 # -A n - no file offset
291 od -A n -t c -t x1
292}
293
294# this isn't special
295# mksh doesn't like it
296echo -n $'\c'' | show_bytes
297
298## STDOUT:
299 \a
300 07
301## END
302
303## BUG bash status: 2
304## BUG bash STDOUT:
305## END
306
307## N-I dash/ash STDOUT:
308## END