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

245 lines, 69 significant
1## oils_failures_allowed: 0
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 parse errors with parse_backslash
215case $SH in (*osh) ;; (*) exit ;; esac
216shopt -s oil:all
217
218const w = c'\uZ'
219
220const x = c'\u{03bc'
221
222# Also invalid
223const y = c'\z'
224
225## stdout-json: ""
226## status: 2
227## N-I dash/bash/mksh/ash status: 0
228
229#### Oil allows unquoted foo\ bar
230shopt -s oil:all
231touch foo\ bar
232ls foo\ bar
233## STDOUT:
234foo bar
235## END
236
237#### $""
238echo $"foo"
239## stdout: foo
240## N-I dash/ash stdout: $foo
241
242#### printf
243# This accepts \t by itself, hm.
244printf "c1\tc2\nc3\tc4\n"
245## stdout-json: "c1\tc2\nc3\tc4\n"