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

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