1 #### single quoted -- implicit and explicit raw
2 var x = 'foo bar'
3 echo $x
4 setvar x = r'foo bar' # Same string
5 echo $x
6 setvar x = r'\t\n' # This is raw
7 echo $x
8 ## STDOUT:
9 foo bar
10 foo bar
11 \t\n
12 ## END
13
14 #### Implicit raw single quote with backslash is a syntax error
15 var x = '\t\n'
16 echo $x
17 ## status: 2
18 ## stdout-json: ""
19
20 #### single quoted C strings: $'foo\n'
21
22 # expression mode
23 var x = $'foo\nbar'
24 echo "$x"
25
26 # command mode
27 if test "$x" = $'foo\nbar'; then
28 echo equal
29 fi
30
31 ## STDOUT:
32 foo
33 bar
34 equal
35 ## END
36
37 #### Double Quoted
38 var name = 'World'
39 var g = "Hello $name"
40
41 echo "Hello $name"
42 echo $g
43 ## STDOUT:
44 Hello World
45 Hello World
46 ## END
47
48 #### Multiline strings with '' and ""
49
50 var single = '
51 single
52 '
53
54 var x = 42
55 var double = "
56 double $x
57 "
58
59 echo $single
60 echo $double
61
62 ## STDOUT:
63
64 single
65
66
67 double 42
68
69 ## END
70
71 #### C strings in %() array literals
72 shopt -s oil:upgrade
73
74 var lines=%($'aa\tbb' $'cc\tdd')
75 write @lines
76
77 ## STDOUT:
78 aa bb
79 cc dd
80 ## END
81
82 #### shopt parse_raw_string
83
84 # Ignored prefix
85 echo r'\'
86
87 # space
88 write r '' end
89
90 # These use shell rules!
91 echo ra'\'
92 echo raw'\'
93
94 echo r"\\"
95
96 # Now it's a regular r
97 shopt --unset parse_raw_string
98 write unset r'\'
99
100 ## STDOUT:
101 \
102 r
103
104 end
105 ra\
106 raw\
107 r\
108 unset
109 r\
110 ## END
111
112 #### Triple Double Quotes, Expression Mode
113
114 var line1 = """line1"""
115 echo line1=$line1
116 var line2 = """
117 line2"""
118 echo line2=$line2
119
120 var two = 2
121 var three = 3
122 var x = """
123 one "
124 two = $two ""
125 three = $three
126 """
127 echo "[$x]"
128
129 var x = """
130 good
131 bad
132 """
133 echo "[$x]"
134
135 ## STDOUT:
136 line1=line1
137 line2=line2
138 [one "
139 two = 2 ""
140 three = 3
141 ]
142 [good
143 bad
144 ]
145 ## END
146
147 #### Triple Single Quotes, Expression Mode
148
149 var two = 2
150 var three = 2
151
152 var x = '''
153 two = $two '
154 three = $three ''
155 \u{61}
156 '''
157 echo "[$x]"
158
159 var x = $'''
160 two = $two '
161 three = $three ''
162 \u{61}
163 '''
164 echo "[$x]"
165
166 ## STDOUT:
167 [two = $two '
168 three = $three ''
169 \u{61}
170 ]
171 [two = $two '
172 three = $three ''
173 a
174 ]
175 ## END
176
177
178 #### Triple Double Quotes, Command Mode
179
180 var two=2
181 var three=3
182
183 echo ""a # test lookahead
184
185 echo --
186 echo """
187 one "
188 two = $two ""
189 three = $three
190 """
191
192 echo --
193 tac <<< """
194 one "
195 two = $two ""
196 three = $three
197 """
198
199 shopt --unset parse_triple_quote
200
201 echo --
202 echo """
203 one
204 two = $two
205 three = $three
206 """
207
208
209 ## STDOUT:
210 a
211 --
212 one "
213 two = 2 ""
214 three = 3
215
216 --
217
218 three = 3
219 two = 2 ""
220 one "
221 --
222
223 one
224 two = 2
225 three = 3
226
227 ## END
228
229 #### raw strings and triple quotes
230
231 echo r'''a'''
232
233 shopt --unset parse_raw_string
234
235 echo r'''a'''
236
237 ## STDOUT:
238 a
239 ra
240 ## END
241
242
243 #### Triple Single Quotes, Command Mode
244
245 echo ''a # make sure lookahead doesn't mess up
246
247 echo --
248 echo '''
249 two = $two
250 '
251 '' '
252 \u{61}
253 '''
254 ## STDOUT:
255 a
256 --
257 two = $two
258 '
259 '' '
260 \u{61}
261
262 ## END
263
264 #### Triple Single Quotes, Here Doc
265
266 tac <<< '''
267 two = $two
268 '
269 '' '
270 \u{61}
271 '''
272
273 ## STDOUT:
274
275 \u{61}
276 '' '
277 '
278 two = $two
279 ## END
280
281 #### Triple Single Quotes, disabled
282
283 shopt --unset parse_triple_quote
284
285 echo '''
286 two = $two
287 \u{61}
288 '''
289
290 ## STDOUT:
291
292 two = $two
293 \u{61}
294
295 ## END
296
297
298 #### $''' in command mode
299
300 echo $'''
301 two = $two
302 '
303 '' '
304 \u{61}
305 '''
306
307 ## STDOUT:
308 two = $two
309 '
310 '' '
311 a
312
313 ## END
314
315 #### here doc with quotes
316
317 # This has 3 right double quotes
318
319 cat <<EOF
320 "hello"
321 ""
322 """
323 EOF
324
325
326 ## STDOUT:
327 "hello"
328 ""
329 """
330 ## END
331
332 #### triple quoted and implicit concatenation
333
334 # Should we allow this? Or I think it's possible to make it a syntax error
335
336 echo '''
337 single
338 '''zz
339
340 echo """
341 double
342 """zz
343 ## status: 2
344 ## stdout-json: ""