1 ## our_shell: ysh
2 ## oils_failures_allowed: 0
3
4 #### single quoted -- implicit and explicit raw
5 var x = 'foo bar'
6 echo $x
7 setvar x = r'foo bar' # Same string
8 echo $x
9 setvar x = r'\t\n' # This is raw
10 echo $x
11 ## STDOUT:
12 foo bar
13 foo bar
14 \t\n
15 ## END
16
17 #### Implicit raw single quote with backslash is a syntax error
18 var x = '\t\n'
19 echo $x
20 ## status: 2
21 ## stdout-json: ""
22
23 #### $"foo $x" to make "foo $x" explicit
24
25 var x = $"bar"
26
27 # expression mode
28 var y = $"foo $x"
29 echo "$y"
30
31 # command mode
32 if test "$y" = $"foo $x"; then
33 echo equal
34 fi
35
36 ## STDOUT:
37 foo bar
38 equal
39 ## END
40
41 #### single quoted C strings: $'foo\n'
42
43 # expression mode
44 var x = $'foo\nbar'
45 echo "$x"
46
47 # command mode
48 if test "$x" = $'foo\nbar'; then
49 echo equal
50 fi
51
52 ## STDOUT:
53 foo
54 bar
55 equal
56 ## END
57
58 #### raw strings and J8 strings don't work in OSH
59 shopt --unset ysh:all
60
61 echo r'hello \'
62 echo u'mu \u{3bc}'
63 echo b'byte \yff'
64
65 echo --
66
67 echo r'''
68 raw multi
69 '''
70
71 echo u'''
72 u multi
73 '''
74
75 echo b'''
76 b multi
77 '''
78
79 ## STDOUT:
80 rhello \
81 umu \u{3bc}
82 bbyte \yff
83 --
84 r
85 raw multi
86
87 u
88 u multi
89
90 b
91 b multi
92
93 ## END
94
95 #### J8-style u'' and b'' strings in expression mode
96
97 var x = u'\u{3bc}'
98 var y = b'\yff'
99
100
101 write --end '' -- $x | od -A n -t x1
102 write --end '' -- $y | od -A n -t x1
103
104 ## STDOUT:
105 ce bc
106 ff
107 ## END
108
109 #### J8-style u'' and b'' strings in command mode
110
111 write --end '' -- u'\u{3bc}' | od -A n -t x1
112 write --end '' -- b'\yff' | od -A n -t x1
113
114 # TODO: make this be illegal
115 # echo u'hello \u03bc'
116
117 ## STDOUT:
118 ce bc
119 ff
120 ## END
121
122 #### J8-style multi-line strings u''' b''' in command mode
123
124 write --end '' -- u'''
125 --
126 \u{61}
127 --
128 '''
129 write --end '' -- b'''
130 --
131 \y62
132 --
133 '''
134
135 # Should be illegal?
136 #echo u'hello \u03bc'
137
138 ## STDOUT:
139 --
140 a
141 --
142 --
143 b
144 --
145 ## END
146
147 #### Double Quoted
148 var name = 'World'
149 var g = "Hello $name"
150
151 echo "Hello $name"
152 echo $g
153 ## STDOUT:
154 Hello World
155 Hello World
156 ## END
157
158 #### Multiline strings with '' and ""
159
160 var single = '
161 single
162 '
163
164 var x = 42
165 var double = "
166 double $x
167 "
168
169 echo $single
170 echo $double
171
172 ## STDOUT:
173
174 single
175
176
177 double 42
178
179 ## END
180
181 #### C strings in %() array literals
182 shopt -s oil:upgrade
183
184 var lines=%($'aa\tbb' $'cc\tdd')
185 write @lines
186
187 ## STDOUT:
188 aa bb
189 cc dd
190 ## END
191
192 #### shopt parse_ysh_string
193
194 # Ignored prefix
195 echo r'\'
196
197 # space
198 write r '' end
199
200 # These use shell rules!
201 echo ra'\'
202 echo raw'\'
203
204 echo r"\\"
205
206 # Now it's a regular r
207 shopt --unset parse_ysh_string
208 write unset r'\'
209
210 ## STDOUT:
211 \
212 r
213
214 end
215 ra\
216 raw\
217 r\
218 unset
219 r\
220 ## END
221
222 #### $''' isn't a a multiline string (removed)
223
224 shopt -s ysh:upgrade
225
226 echo $'''
227 foo
228 '''
229
230 ## STDOUT:
231
232 foo
233
234 ## END
235
236
237 #### """ and $""" in Expression Mode
238
239 var line1 = """line1"""
240 echo line1=$line1
241 var line2 = """
242 line2"""
243 echo line2=$line2
244
245 var two = 2
246 var three = 3
247 var x = """
248 one "
249 two = $two ""
250 three = $three
251 """
252 echo "[$x]"
253
254 var i = 42
255 var x = """
256 good
257 bad $i
258 """
259 echo "[$x]"
260
261 # alias
262 var x = $"""
263 good
264 bad $i
265 """
266 echo "[$x]"
267
268 ## STDOUT:
269 line1=line1
270 line2=line2
271 [one "
272 two = 2 ""
273 three = 3
274 ]
275 [good
276 bad 42
277 ]
278 [good
279 bad 42
280 ]
281 ## END
282
283 #### ''' in Expression Mode
284
285 var two = 2
286 var three = 2
287
288 var x = '''
289 two = $two '
290 three = $three ''
291 \u{61}
292 '''
293 echo "[$x]"
294
295 var x = u'''
296 two = $two '
297 three = $three ''
298 \u{61}
299 '''
300 echo "[$x]"
301
302 var x = b'''
303 two = $two '
304 three = $three ''
305 \u{61} \y61
306 '''
307 echo "[$x]"
308
309 ## STDOUT:
310 [two = $two '
311 three = $three ''
312 \u{61}
313 ]
314 [two = $two '
315 three = $three ''
316 a
317 ]
318 [two = $two '
319 three = $three ''
320 a a
321 ]
322 ## END
323
324
325 #### """ and $""" in Command Mode
326
327 var two=2
328 var three=3
329
330 echo ""a # test lookahead
331
332 echo --
333 echo """
334 one "
335 two = $two ""
336 three = $three
337 """
338
339 # optional $ prefix
340 echo --
341 echo $"""
342 one "
343 two = $two ""
344 three = $three
345 """
346
347 echo --
348 tac <<< """
349 one "
350 two = $two ""
351 three = $three
352 """
353
354 shopt --unset parse_triple_quote
355
356 echo --
357 echo """
358 one
359 two = $two
360 three = $three
361 """
362
363
364 ## STDOUT:
365 a
366 --
367 one "
368 two = 2 ""
369 three = 3
370
371 --
372 one "
373 two = 2 ""
374 three = 3
375
376 --
377
378 three = 3
379 two = 2 ""
380 one "
381 --
382
383 one
384 two = 2
385 three = 3
386
387 ## END
388
389
390 #### ''' in Command Mode
391
392 echo ''a # make sure lookahead doesn't mess up
393
394 echo --
395 echo '''
396 two = $two
397 '
398 '' '
399 \u{61}
400 '''
401 ## STDOUT:
402 a
403 --
404 two = $two
405 '
406 '' '
407 \u{61}
408
409 ## END
410
411 #### r''' in Command Mode, Expression mode
412
413 echo r'''\'''
414
415 var x = r'''\'''
416 echo $x
417
418 shopt --unset parse_ysh_string
419
420 echo r'''\'''
421
422 ## STDOUT:
423 \
424 \
425 r\
426 ## END
427
428
429 #### ''' in Here Doc
430
431 tac <<< '''
432 two = $two
433 '
434 '' '
435 \u{61}
436 '''
437
438 ## STDOUT:
439
440 \u{61}
441 '' '
442 '
443 two = $two
444 ## END
445
446 #### ''' without parse_triple_quote
447
448 shopt --unset parse_triple_quote
449
450 echo '''
451 two = $two
452 \u{61}
453 '''
454
455 ## STDOUT:
456
457 two = $two
458 \u{61}
459
460 ## END
461
462 #### here doc with quotes
463
464 # This has 3 right double quotes
465
466 cat <<EOF
467 "hello"
468 ""
469 """
470 EOF
471
472
473 ## STDOUT:
474 "hello"
475 ""
476 """
477 ## END
478
479 #### triple quoted and implicit concatenation
480
481 # Should we allow this? Or I think it's possible to make it a syntax error
482
483 echo '''
484 single
485 '''zz
486
487 echo """
488 double
489 """zz
490 ## status: 2
491 ## stdout-json: ""