OILS / spec / var-op-bash.test.sh View on Github | oils.pub

546 lines, 303 significant
1## compare_shells: bash
2## oils_failures_allowed: 7
3
4#### Lower Case with , and ,,
5x='ABC DEF'
6echo ${x,}
7echo ${x,,}
8echo empty=${empty,}
9echo empty=${empty,,}
10## STDOUT:
11aBC DEF
12abc def
13empty=
14empty=
15## END
16
17#### Upper Case with ^ and ^^
18x='abc def'
19echo ${x^}
20echo ${x^^}
21echo empty=${empty^}
22echo empty=${empty^^}
23## STDOUT:
24Abc def
25ABC DEF
26empty=
27empty=
28## END
29
30#### Case folding - Unicode characters
31
32# https://www.utf8-chartable.de/unicode-utf8-table.pl
33
34x=$'\u00C0\u00C8' # upper grave
35y=$'\u00E1\u00E9' # lower acute
36
37echo u ${x^}
38echo U ${x^^}
39
40echo l ${x,}
41echo L ${x,,}
42
43echo u ${y^}
44echo U ${y^^}
45
46echo l ${y,}
47echo L ${y,,}
48
49## STDOUT:
50u ÀÈ
51U ÀÈ
52l àÈ
53L àè
54u Áé
55U ÁÉ
56l áé
57L áé
58## END
59
60#### Case folding - multi code point
61
62echo shell
63small=$'\u00DF'
64echo u ${small^}
65echo U ${small^^}
66
67echo l ${small,}
68echo L ${small,,}
69echo
70
71echo python2
72python2 -c '
73small = u"\u00DF"
74print(small.upper().encode("utf-8"))
75print(small.lower().encode("utf-8"))
76'
77echo
78
79# Not in the container images, but python 3 DOES support it!
80# This is moved to demo/survey-case-fold.sh
81
82if false; then
83echo python3
84python3 -c '
85import sys
86small = u"\u00DF"
87sys.stdout.buffer.write(small.upper().encode("utf-8") + b"\n")
88sys.stdout.buffer.write(small.lower().encode("utf-8") + b"\n")
89'
90fi
91
92if false; then
93 # Yes, supported
94 echo node.js
95
96 nodejs -e '
97 var small = "\u00DF"
98 console.log(small.toUpperCase())
99 console.log(small.toLowerCase())
100 '
101fi
102
103## STDOUT:
104## END
105## BUG bash STDOUT:
106shell
107u ß
108U ß
109l ß
110L ß
111
112python2
113ß
114ß
115
116## END
117
118#### Case folding that depends on locale (not enabled, requires Turkish locale)
119
120# Hm this works in demo/survey-case-fold.sh
121# Is this a bash 4.4 thing?
122
123#export LANG='tr_TR.UTF-8'
124#echo $LANG
125
126x='i'
127
128echo u ${x^}
129echo U ${x^^}
130
131echo l ${x,}
132echo L ${x,,}
133
134## OK bash/osh STDOUT:
135u I
136U I
137l i
138L i
139## END
140
141#### Lower Case with constant string (VERY WEIRD)
142x='AAA ABC DEF'
143echo ${x,A}
144echo ${x,,A} # replaces every A only?
145## STDOUT:
146aAA ABC DEF
147aaa aBC DEF
148## END
149
150#### Lower Case glob
151
152# Hm with C.UTF-8, this does no case folding?
153export LC_ALL=en_US.UTF-8
154
155x='ABC DEF'
156echo ${x,[d-f]}
157echo ${x,,[d-f]} # bash 4.4 fixed in bash 5.2.21
158## STDOUT:
159ABC DEF
160ABC DEF
161## END
162
163#### ${x@u} U L - upper / lower case (bash 5.1 feature)
164
165# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
166
167x='abc DEF'
168
169echo "${x@u}"
170
171echo "${x@U}"
172
173echo "${x@L}"
174
175## STDOUT:
176Abc DEF
177ABC DEF
178abc def
179## END
180
181
182#### ${x@Q}
183x="FOO'BAR spam\"eggs"
184eval "new=${x@Q}"
185test "$x" = "$new" && echo OK
186## STDOUT:
187OK
188## END
189
190#### ${array@Q} and ${array[@]@Q}
191array=(x 'y\nz')
192echo ${array[@]@Q}
193echo ${array@Q}
194echo ${array@Q}
195## STDOUT:
196'x' 'y\nz'
197'x'
198'x'
199## END
200## OK osh STDOUT:
201x $'y\\nz'
202x
203x
204## END
205
206#### ${!prefix@} ${!prefix*} yields sorted array of var names
207ZOO=zoo
208ZIP=zip
209ZOOM='one two'
210Z='three four'
211
212z=lower
213
214argv.py ${!Z*}
215argv.py ${!Z@}
216argv.py "${!Z*}"
217argv.py "${!Z@}"
218for i in 1 2; do argv.py ${!Z*} ; done
219for i in 1 2; do argv.py ${!Z@} ; done
220for i in 1 2; do argv.py "${!Z*}"; done
221for i in 1 2; do argv.py "${!Z@}"; done
222## STDOUT:
223['Z', 'ZIP', 'ZOO', 'ZOOM']
224['Z', 'ZIP', 'ZOO', 'ZOOM']
225['Z ZIP ZOO ZOOM']
226['Z', 'ZIP', 'ZOO', 'ZOOM']
227['Z', 'ZIP', 'ZOO', 'ZOOM']
228['Z', 'ZIP', 'ZOO', 'ZOOM']
229['Z', 'ZIP', 'ZOO', 'ZOOM']
230['Z', 'ZIP', 'ZOO', 'ZOOM']
231['Z ZIP ZOO ZOOM']
232['Z ZIP ZOO ZOOM']
233['Z', 'ZIP', 'ZOO', 'ZOOM']
234['Z', 'ZIP', 'ZOO', 'ZOOM']
235## END
236
237#### ${!prefix@} matches var name (regression)
238hello1=1 hello2=2 hello3=3
239echo ${!hello@}
240hello=()
241echo ${!hello@}
242## STDOUT:
243hello1 hello2 hello3
244hello hello1 hello2 hello3
245## END
246
247#### ${var@a} for attributes
248array=(one two)
249echo ${array@a}
250declare -r array=(one two)
251echo ${array@a}
252declare -rx PYTHONPATH=hi
253echo ${PYTHONPATH@a}
254
255# bash and osh differ here
256#declare -rxn x=z
257#echo ${x@a}
258## STDOUT:
259a
260ar
261rx
262## END
263
264#### ${var@a} error conditions
265echo [${?@a}]
266## STDOUT:
267[]
268## END
269
270#### undef and @P @Q @a
271$SH -c 'echo ${undef@P}'
272echo status=$?
273$SH -c 'echo ${undef@Q}'
274echo status=$?
275$SH -c 'echo ${undef@a}'
276echo status=$?
277## STDOUT:
278
279status=0
280
281status=0
282
283status=0
284## END
285
286
287#### argv array and @P @Q @a
288$SH -c 'echo ${@@P}' dummy a b c
289echo status=$?
290$SH -c 'echo ${@@Q}' dummy a 'b\nc'
291echo status=$?
292$SH -c 'echo ${@@a}' dummy a b c
293echo status=$?
294## STDOUT:
295a b c
296status=0
297'a' 'b\nc'
298status=0
299
300status=0
301## END
302## OK osh STDOUT:
303a b c
304status=0
305a $'b\\nc'
306status=0
307a a a
308status=0
309## END
310
311#### assoc array and @P @Q @a
312
313# note: "y z" causes a bug!
314$SH -c 'declare -A A=(["x"]="y"); echo ${A@P} - ${A[@]@P}'
315echo status=$?
316
317# note: "y z" causes a bug!
318$SH -c 'declare -A A=(["x"]="y"); echo ${A@Q} - ${A[@]@Q}' | sed 's/^- y$/- '\''y'\''/'
319echo status=$?
320
321$SH -c 'declare -A A=(["x"]=y); echo ${A@a} - ${A[@]@a}'
322echo status=$?
323## STDOUT:
324- y
325status=0
326- 'y'
327status=0
328A - A
329status=0
330## END
331
332#### ${!var[@]@X}
333# note: "y z" causes a bug!
334$SH -c 'declare -A A=(["x"]="y"); echo ${!A[@]@P}'
335if test $? -ne 0; then echo fail; fi
336
337# note: "y z" causes a bug!
338$SH -c 'declare -A A=(["x y"]="y"); echo ${!A[@]@Q}'
339if test $? -ne 0; then echo fail; fi
340
341$SH -c 'declare -A A=(["x"]=y); echo ${!A[@]@a}'
342if test $? -ne 0; then echo fail; fi
343# STDOUT:
344
345
346
347# END
348## OK osh STDOUT:
349fail
350'x y'
351a
352## END
353
354#### ${#var@X} is a parse error
355# note: "y z" causes a bug!
356$SH -c 'declare -A A=(["x"]="y"); echo ${#A[@]@P}'
357if test $? -ne 0; then echo fail; fi
358
359# note: "y z" causes a bug!
360$SH -c 'declare -A A=(["x"]="y"); echo ${#A[@]@Q}'
361if test $? -ne 0; then echo fail; fi
362
363$SH -c 'declare -A A=(["x"]=y); echo ${#A[@]@a}'
364if test $? -ne 0; then echo fail; fi
365## STDOUT:
366fail
367fail
368fail
369## END
370
371#### ${!A@a} and ${!A[@]@a}
372declare -A A=(["x"]=y)
373echo x=${!A[@]@a}
374echo invalid=${!A@a}
375
376# OSH prints 'a' for indexed array because the AssocArray with ! turns into
377# it. Disallowing it would be the other reasonable behavior.
378
379## status: 1
380## STDOUT:
381x=
382## END
383
384# Bash succeeds with ${!A@a}, which references the variable named as $A (i.e.,
385# ''). This must be a Bash bug since the behavior is inconsistent with the
386# fact that ${!undef@a} and ${!empty@a} fail.
387
388## BUG bash status: 0
389## BUG bash STDOUT:
390x=
391invalid=
392## END
393
394#### undef vs. empty string in var ops
395
396empty=''
397x=x
398
399echo ${x@Q} ${empty@Q} ${undef@Q} ${x@Q}
400
401echo ${x@K} ${empty@K} ${undef@K} ${x@K}
402
403echo ${x@k} ${empty@k} ${undef@k} ${x@k}
404
405echo ${x@A} ${empty@A} ${undef@A} ${x@A}
406
407declare -r x
408echo ${x@a} ${empty@a} ${undef@a} ${x@a}
409
410# x x
411#echo ${x@E} ${empty@E} ${undef@E} ${x@E}
412# x x
413#echo ${x@P} ${empty@P} ${undef@P} ${x@P}
414
415## STDOUT:
416'x' '' 'x'
417'x' '' 'x'
418'x' '' 'x'
419x='x' empty='' x='x'
420r r
421## END
422
423#### -o nounset with var ops
424
425set -u
426(echo ${undef@Q}); echo "stat: $?"
427(echo ${undef@P}); echo "stat: $?"
428(echo ${undef@a}); echo "stat: $?"
429
430## STDOUT:
431stat: 1
432stat: 1
433stat: 1
434## END
435
436
437#### ${a[0]@a} and ${a@a}
438
439a=(1 2 3)
440echo "attr = '${a[0]@a}'"
441echo "attr = '${a@a}'"
442
443## STDOUT:
444attr = 'a'
445attr = 'a'
446## END
447
448
449#### ${!r@a} with r='a[0]' (attribute for indirect expansion of an array element)
450
451a=(1 2 3)
452r='a'
453echo ${!r@a}
454r='a[0]'
455echo ${!r@a}
456
457declare -A d=([0]=foo [1]=bar)
458r='d'
459echo ${!r@a}
460r='d[0]'
461echo ${!r@a}
462
463## STDOUT:
464a
465a
466A
467A
468## END
469
470
471#### Array expansion with nullary var ops
472
473declare -a a=({1..9})
474declare -A A=(['a']=hello ['b']=world ['c']=osh ['d']=ysh)
475
476echo "@Q"
477argv.py "${a[@]@Q}"
478argv.py "${a[*]@Q}"
479argv.py "${A[@]@Q}"
480argv.py "${A[*]@Q}"
481argv.py "${u[@]@Q}"
482argv.py "${u[*]@Q}"
483
484echo "@P"
485argv.py "${a[@]@P}"
486argv.py "${a[*]@P}"
487argv.py "${A[@]@P}"
488argv.py "${A[*]@P}"
489argv.py "${u[@]@P}"
490argv.py "${u[*]@P}"
491
492echo "@a"
493argv.py "${a[@]@a}"
494argv.py "${a[*]@a}"
495argv.py "${A[@]@a}"
496argv.py "${A[*]@a}"
497argv.py "${u[@]@a}"
498argv.py "${u[*]@a}"
499
500## STDOUT:
501@Q
502['1', '2', '3', '4', '5', '6', '7', '8', '9']
503['1 2 3 4 5 6 7 8 9']
504['hello', 'world', 'osh', 'ysh']
505['hello world osh ysh']
506[]
507['']
508@P
509['1', '2', '3', '4', '5', '6', '7', '8', '9']
510['1 2 3 4 5 6 7 8 9']
511['hello', 'world', 'osh', 'ysh']
512['hello world osh ysh']
513[]
514['']
515@a
516['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
517['a a a a a a a a a']
518['A', 'A', 'A', 'A']
519['A A A A']
520[]
521['']
522## END
523
524## OK bash STDOUT:
525@Q
526["'1'", "'2'", "'3'", "'4'", "'5'", "'6'", "'7'", "'8'", "'9'"]
527["'1' '2' '3' '4' '5' '6' '7' '8' '9'"]
528["'ysh'", "'osh'", "'world'", "'hello'"]
529["'ysh' 'osh' 'world' 'hello'"]
530[]
531['']
532@P
533['1', '2', '3', '4', '5', '6', '7', '8', '9']
534['1 2 3 4 5 6 7 8 9']
535['ysh', 'osh', 'world', 'hello']
536['ysh osh world hello']
537[]
538['']
539@a
540['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
541['a a a a a a a a a']
542['A', 'A', 'A', 'A']
543['A A A A']
544[]
545['']
546## END