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