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 ysh: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 # Now it's a regular r
258 shopt --unset parse_ysh_string
259 shopt --set parse_word_join
260 write unset r'\'
261
262 ## STDOUT:
263 \
264 r
265
266 end
267 unset
268 r\
269 ## END
270
271 #### Special rule for <<< ''' and <<< """ - no extra newline
272
273 read --all <<< unquoted
274 pp test_ (_reply)
275
276 read --all <<< 'single with newline'
277 pp test_ (_reply)
278
279 read --all <<< "double with newline"
280 pp test_ (_reply)
281
282 read --all <<< u'j8 with newline'
283 pp test_ (_reply)
284
285 echo
286
287 read --all <<< '''
288 multi
289 single
290 '''
291 pp test_ (_reply)
292
293 read --all <<< """
294 multi
295 double
296 """
297 pp test_ (_reply)
298
299 read --all <<< u'''
300 multi
301 j8
302 '''
303 pp test_ (_reply)
304
305
306 ## STDOUT:
307 (Str) "unquoted\n"
308 (Str) "single with newline\n"
309 (Str) "double with newline\n"
310 (Str) "j8 with newline\n"
311
312 (Str) "multi\nsingle\n"
313 (Str) "multi\ndouble\n"
314 (Str) "multi\nj8\n"
315 ## END
316
317 #### $''' isn't a a multiline string (removed)
318
319 shopt -s ysh:upgrade
320
321 echo $'''
322 foo
323 '''
324
325 ## status: 2
326 ## STDOUT:
327 ## END
328
329
330 #### """ and $""" in Expression Mode
331
332 var line1 = """line1"""
333 echo line1=$line1
334 var line2 = """
335 line2"""
336 echo line2=$line2
337
338 var two = 2
339 var three = 3
340 var x = """
341 one "
342 two = $two ""
343 three = $three
344 """
345 echo "[$x]"
346
347 var i = 42
348 var x = """
349 good
350 bad $i
351 """
352 echo "[$x]"
353
354 # alias
355 var x = $"""
356 good
357 bad $i
358 """
359 echo "[$x]"
360
361 ## STDOUT:
362 line1=line1
363 line2=line2
364 [one "
365 two = 2 ""
366 three = 3
367 ]
368 [good
369 bad 42
370 ]
371 [good
372 bad 42
373 ]
374 ## END
375
376 #### ''' in Expression Mode
377
378 var two = 2
379 var three = 2
380
381 var x = '''
382 two = $two '
383 three = $three ''
384 \u{61}
385 '''
386 echo "[$x]"
387
388 var x = u'''
389 two = $two '
390 three = $three ''
391 \u{61}
392 '''
393 echo "[$x]"
394
395 var x = b'''
396 two = $two '
397 three = $three ''
398 \u{61} \y61
399 '''
400 echo "[$x]"
401
402 ## STDOUT:
403 [two = $two '
404 three = $three ''
405 \u{61}
406 ]
407 [two = $two '
408 three = $three ''
409 a
410 ]
411 [two = $two '
412 three = $three ''
413 a a
414 ]
415 ## END
416
417
418 #### """ and $""" in Command Mode
419
420 var two=2
421 var three=3
422
423 echo --
424 echo """
425 one "
426 two = $two ""
427 three = $three
428 """
429
430 # optional $ prefix
431 echo --
432 echo $"""
433 one "
434 two = $two ""
435 three = $three
436 """
437
438 echo --
439 tac <<< """
440 one "
441 two = $two ""
442 three = $three
443 """
444
445 ## STDOUT:
446 --
447 one "
448 two = 2 ""
449 three = 3
450
451 --
452 one "
453 two = 2 ""
454 three = 3
455
456 --
457 three = 3
458 two = 2 ""
459 one "
460 ## END
461
462
463 #### ''' in Command Mode
464
465 echo --
466 echo '''
467 two = $two
468 '
469 '' '
470 \u{61}
471 '''
472 ## STDOUT:
473 --
474 two = $two
475 '
476 '' '
477 \u{61}
478
479 ## END
480
481 #### r''' in Command Mode, Expression mode
482
483 echo r'''\'''
484
485 var x = r'''\'''
486 echo $x
487
488 shopt --unset parse_ysh_string
489 shopt --set parse_word_join
490
491 echo r'''\'''
492
493 ## STDOUT:
494 \
495 \
496 r\
497 ## END
498
499
500 #### ''' in Here Doc
501
502 tac <<< '''
503 two = $two
504 '
505 '' '
506 \u{61}
507 '''
508
509 ## STDOUT:
510 \u{61}
511 '' '
512 '
513 two = $two
514 ## END
515
516 #### ''' without parse_triple_quote
517
518 shopt --unset parse_triple_quote
519
520 echo '''
521 two = $two
522 \u{61}
523 '''
524
525 ## status: 2
526 ## STDOUT:
527 ## END
528
529 #### here doc with quotes
530
531 # This has 3 right double quotes
532
533 cat <<EOF
534 "hello"
535 ""
536 """
537 EOF
538
539
540 ## STDOUT:
541 "hello"
542 ""
543 """
544 ## END
545
546 #### triple quoted and implicit concatenation
547
548 # Should we allow this? Or I think it's possible to make it a syntax error
549
550 echo '''
551 single
552 '''zz
553
554 echo """
555 double
556 """zz
557 ## status: 2
558 ## stdout-json: ""
559