OILS / spec / quote.test.sh View on Github | oilshell.org

231 lines, 59 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-json: "newline\ninside single-quoted string\n"
24
25#### Double-quoted
26echo "double quoted"
27## stdout: double quoted
28
29#### Mix of quotes in one word
30echo unquoted' single-quoted'" double-quoted "unquoted
31## stdout: unquoted single-quoted double-quoted unquoted
32
33#### Var substitution
34FOO=bar
35echo "==$FOO=="
36## stdout: ==bar==
37
38#### Var substitution with braces
39FOO=bar
40echo foo${FOO}
41## stdout: foobar
42
43#### Var substitution with braces, quoted
44FOO=bar
45echo "foo${FOO}"
46## stdout: foobar
47
48#### Var length
49FOO=bar
50echo "foo${#FOO}"
51## stdout: foo3
52
53#### Storing backslashes and then echoing them
54# This is a bug fix; it used to cause problems with unescaping.
55one='\'
56two='\\'
57echo $one $two
58echo "$one" "$two"
59## STDOUT:
60\ \\
61\ \\
62## END
63## BUG dash/mksh STDOUT:
64\ \
65\ \
66## END
67
68#### Backslash escapes
69echo \$ \| \a \b \c \d \\
70## stdout: $ | a b c d \
71
72#### Backslash escapes inside double quoted string
73echo "\$ \\ \\ \p \q"
74## stdout: $ \ \ \p \q
75
76#### C-style backslash escapes inside double quoted string
77# mksh and dash implement POSIX incompatible extensions. $ ` " \ <newline>
78# are the only special ones
79echo "\a \b"
80## stdout: \a \b
81## BUG dash/mksh stdout-json: "\u0007 \u0008\n"
82
83# BUG
84
85#### Literal $
86echo $
87## stdout: $
88
89#### Quoted Literal $
90echo $ "$" $
91## stdout: $ $ $
92
93#### Line continuation
94echo foo\
95$
96## stdout: foo$
97
98#### Line continuation inside double quotes
99echo "foo\
100$"
101## stdout: foo$
102
103#### $? split over multiple lines
104# Same with $$, etc. OSH won't do this because $? is a single token.
105echo $\
106?
107## stdout: $?
108## OK dash/bash/mksh/ash stdout: 0
109
110#
111# Bad quotes
112#
113
114# TODO: Also test unterminated quotes inside ${} and $()
115
116#### Unterminated single quote
117## code: ls foo bar '
118## status: 2
119## OK mksh status: 1
120
121#### Unterminated double quote
122## code: ls foo bar "
123## status: 2
124## OK mksh status: 1
125
126
127#
128# TODO: Might be another section?
129#
130
131#### Semicolon
132echo separated; echo by semi-colon
133## stdout-json: "separated\nby semi-colon\n"
134
135#
136# TODO: Variable substitution operators.
137#
138
139#### No tab escapes within single quotes
140# dash and mksh allow this, which is a BUG.
141# POSIX says: "Enclosing characters in single-quotes ( '' ) shall preserve the
142# literal value of each character within the single-quotes. A single-quote
143# cannot occur within single-quotes"
144echo 'a\tb'
145## stdout: a\tb
146## BUG dash/mksh stdout-json: "a\tb\n"
147
148# See if it supports ANSI C escapes. Bash supports this, but dash does NOT. I
149# guess dash you would do IFS=$(printf '\n\t')
150
151#### $''
152echo $'foo'
153## stdout: foo
154## N-I dash stdout: $foo
155
156#### $'' with quotes
157echo $'single \' double \"'
158## stdout: single ' double "
159## N-I dash stdout-json: ""
160## N-I dash status: 2
161
162#### $'' with newlines
163echo $'col1\ncol2\ncol3'
164## stdout-json: "col1\ncol2\ncol3\n"
165# In dash, \n is special within single quotes
166## N-I dash stdout-json: "$col1\ncol2\ncol3\n"
167
168#### $'' octal escapes don't have leading 0
169# echo -e syntax is echo -e \0377
170echo -n $'\001' $'\377' | od -A n -c | sed 's/ \+/ /g'
171## STDOUT:
172 001 377
173## END
174## N-I dash STDOUT:
175 $ 001 $ 377
176## END
177
178#### $'' octal escapes with fewer than 3 chars
179echo $'\1 \11 \11 \111' | od -A n -c | sed 's/ \+/ /g'
180## STDOUT:
181 001 \t \t I \n
182## END
183## N-I dash STDOUT:
184 $ 001 \t \t I \n
185## END
186
187
188#### OSH allows invalid backslashes
189case $SH in (dash|mksh) exit ;; esac
190
191w=$'\uZ'
192x=$'\u{03bc'
193y=$'\z'
194echo $w $x $y
195## STDOUT:
196\uZ \u{03bc \z
197## END
198## N-I dash/mksh stdout-json: ""
199
200#### YSH parse errors with parse_backslash
201case $SH in (*osh) ;; (*) exit ;; esac
202shopt -s oil:all
203
204const w = c'\uZ'
205
206const x = c'\u{03bc'
207
208# Also invalid
209const y = c'\z'
210
211## stdout-json: ""
212## status: 2
213## N-I dash/bash/mksh/ash status: 0
214
215#### Oil allows unquoted foo\ bar
216shopt -s oil:all
217touch foo\ bar
218ls foo\ bar
219## STDOUT:
220foo bar
221## END
222
223#### $""
224echo $"foo"
225## stdout: foo
226## N-I dash/ash stdout: $foo
227
228#### printf
229# This accepts \t by itself, hm.
230printf "c1\tc2\nc3\tc4\n"
231## stdout-json: "c1\tc2\nc3\tc4\n"