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