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

767 lines, 464 significant
1## compare_shells: bash
2
3# Var refs are done with ${!a}
4#
5# local/declare -n is tested in spec/named-ref.test.sh.
6#
7# http://stackoverflow.com/questions/16461656/bash-how-to-pass-array-as-an-argument-to-a-function
8
9#### var ref ${!a}
10a=b
11b=c
12echo ref ${!a} ${a}
13## stdout: ref c b
14
15#### ${!ref-default}
16ref=x
17echo x=${!ref-default}
18
19x=''
20echo x=${!ref-default}
21
22x=foo
23echo x=${!ref-default}
24
25## STDOUT:
26x=default
27x=
28x=foo
29## END
30
31#### ${!undef:-}
32# bash 4.4 gives empty string, but I feel like this could be an error
33echo undef=${!undef-'default'}
34echo undef=${!undef}
35
36set -u
37echo NOUNSET
38echo undef=${!undef-'default'}
39echo undef=${!undef}
40
41## status: 1
42## STDOUT:
43NOUNSET
44## END
45
46# Bash 4.4 had been generating an empty string, but it was fixed in Bash 5.0.
47#
48# ## BUG bash STDOUT:
49# undef=default
50# undef=
51# NOUNSET
52# undef=default
53# ## END
54
55#### comparison to ${!array[@]} keys (similar SYNTAX)
56
57declare -a a=(x y)
58argv.py "${!a[@]}"
59echo a_keys=$?
60
61argv.py "${!a}" # missing [] is equivalent to ${!a[0]} ?
62echo a_nobrackets=$?
63
64echo ---
65declare -A A=([A]=a [B]=b)
66
67argv.py $(printf '%s\n' ${!A[@]} | sort)
68echo A_keys=$?
69
70(argv.py "${!A}") # missing [] is equivalent to ${!A[0]} ?
71echo A_nobrackets=$?
72
73## STDOUT:
74['0', '1']
75a_keys=0
76['']
77a_nobrackets=0
78---
79['A', 'B']
80A_keys=0
81A_nobrackets=1
82## END
83
84## BUG bash STDOUT:
85['0', '1']
86a_keys=0
87['']
88a_nobrackets=0
89---
90['A', 'B']
91A_keys=0
92['']
93A_nobrackets=0
94## END
95
96#### ${!a[@]-'default'} is legal but fails with more than one element
97
98# bash allows this construct, but the indirection fails when the array has more
99# than one element because the variable name contains a space. OSH originally
100# made it an error unconditionally because [@] implies it's an array, so the
101# behavior has been different from Bash when the array has a single element.
102# We now changed it to follow Bash even when the array has a single element.
103
104(argv.py "${!a[@]-default}")
105echo status=$?
106
107a=(x y z)
108(argv.py "${!a[@]-default}")
109echo status=$?
110## status: 0
111## STDOUT:
112status=1
113status=1
114## END
115
116# Bash 4.4 had been generating an empty string for ${!undef[@]-}, but this was
117# fixed in Bash 5.0.
118#
119# ## BUG bash status: 0
120# ## BUG bash STDOUT:
121# ['default']
122# status=0
123# status=1
124# ## END
125
126
127#### var ref to $@ with @
128set -- one two
129ref='@'
130echo ref=${!ref}
131## STDOUT:
132ref=one two
133## END
134
135#### var ref to $1 and $2 with 1 and 2
136set -- one two
137ref1='1'
138echo ref1=${!ref1}
139ref2='2'
140echo ref2=${!ref2}
141
142## STDOUT:
143ref1=one
144ref2=two
145## END
146
147#### var ref: 1, @, *
148set -- x y
149ref=1; argv.py "${!ref}"
150ref=@; argv.py "${!ref}"
151ref=*; argv.py "${!ref}" # maybe_decay_array bug?
152
153## STDOUT:
154['x']
155['x', 'y']
156['x y']
157## END
158
159#### var ref to special var BASH_SOURCE
160ref='LINENO'
161echo lineno=${!ref}
162## STDOUT:
163lineno=2
164## END
165
166#### var ref to $? with '?'
167myfunc() {
168 local ref=$1
169 echo ${!ref}
170}
171myfunc FUNCNAME
172myfunc '?'
173## STDOUT:
174myfunc
1750
176## END
177
178
179#### Var ref, then assignment with ${ := }
180z=zz
181zz=
182echo ${!z:=foo}
183echo ${!z:=bar}
184## STDOUT:
185foo
186foo
187## END
188
189#### Var ref, then error with ${ ? }
190w=ww
191ww=
192echo ${!w:?'my message'}
193echo done
194## status: 1
195## STDOUT:
196## END
197
198#### Indirect expansion, THEN suffix operators
199
200check_eq() {
201 [ "$1" = "$2" ] || { echo "$1 vs $2"; }
202}
203check_expand() {
204 val=$(eval "echo \"$1\"")
205 [ "$val" = "$2" ] || { echo "$1 -> expected $2, got $val"; }
206}
207check_err() {
208 e="$1"
209 msg=$(eval "$e" 2>&1) && echo "bad success: $e"
210 if test -n "$2"; then
211 if [[ "$msg" != $2 ]]; then
212 echo "Expected error: $e"
213 echo "Got error : $msg"
214 fi
215 fi
216}
217# Nearly everything in manual section 3.5.3 "Shell Parameter Expansion"
218# is allowed after a !-indirection.
219#
220# Not allowed: any further prefix syntax.
221x=xx; xx=aaabcc
222xd=x
223check_err '${!!xd}'
224check_err '${!!x*}'
225a=(asdf x)
226check_err '${!!a[*]}'
227check_err '${!#x}'
228check_err '${!#a[@]}'
229# And an array reference binds tighter in the syntax, so goes first;
230# there's no way to spell "indirection, then array reference".
231check_expand '${!a[1]}' xx
232b=(aoeu a)
233check_expand '${!b[1]}' asdf # i.e. like !(b[1]), not (!b)[1]
234#
235# Allowed: apparently everything else.
236y=yy; yy=
237check_expand '${!y:-foo}' foo
238check_expand '${!x:-foo}' aaabcc
239
240check_expand '${!x:?oops}' aaabcc
241
242check_expand '${!y:+foo}' ''
243check_expand '${!x:+foo}' foo
244
245check_expand '${!x:2}' abcc
246check_expand '${!x:2:2}' ab
247
248check_expand '${!x#*a}' aabcc
249check_expand '${!x%%c*}' aaab
250check_expand '${!x/a*b/d}' dcc
251
252# ^ operator not fully implemented in OSH
253#check_expand '${!x^a}' Aaabcc
254
255p=pp; pp='\$ '
256check_expand '${!p@P}' '$ '
257echo ok
258## stdout: ok
259
260#### var ref OF array var -- silent a[0] decay
261declare -a a=(ale bean)
262echo first=${!a}
263
264ale=zzz
265echo first=${!a}
266
267## status: 0
268## STDOUT:
269first=
270first=zzz
271## END
272
273#### array ref
274
275declare -a array=(ale bean)
276ref='array[0]'
277echo ${!ref}
278## status: 0
279## STDOUT:
280ale
281## END
282
283#### array ref with strict_array
284shopt -s strict_array
285
286declare -a array=(ale bean)
287ref='array'
288echo ${!ref}
289## status: 1
290## stdout-json: ""
291## N-I bash status: 0
292## N-I bash STDOUT:
293ale
294## END
295
296#### var ref TO array var
297shopt -s compat_array
298
299declare -a array=(ale bean)
300
301ref='array' # when compat_array is on, this is like array[0]
302ref_AT='array[@]'
303
304echo ${!ref}
305echo ${!ref_AT}
306
307## STDOUT:
308ale
309ale bean
310## END
311
312#### var ref TO array var, with subscripts
313f() {
314 argv.py "${!1}"
315}
316f 'nonexistent[0]'
317array=(x y z)
318f 'array[0]'
319f 'array[1+1]'
320f 'array[@]'
321f 'array[*]'
322# Also associative arrays.
323## STDOUT:
324['']
325['x']
326['z']
327['x', 'y', 'z']
328['x y z']
329## END
330
331#### var ref TO assoc array a[key]
332shopt -s compat_array
333
334declare -A assoc=([ale]=bean [corn]=dip)
335ref=assoc
336#ref_AT='assoc[@]'
337
338# UNQUOTED doesn't work with Oil's parser
339#ref_SUB='assoc[ale]'
340ref_SUB='assoc["ale"]'
341
342ref_SUB_QUOTED='assoc["al"e]'
343
344ref_SUB_BAD='assoc["bad"]'
345
346echo ref=${!ref} # compat_array: assoc is equivalent to assoc[0]
347#echo ref_AT=${!ref_AT}
348echo ref_SUB=${!ref_SUB}
349echo ref_SUB_QUOTED=${!ref_SUB_QUOTED}
350echo ref_SUB_BAD=${!ref_SUB_BAD}
351
352## STDOUT:
353ref=
354ref_SUB=bean
355ref_SUB_QUOTED=bean
356ref_SUB_BAD=
357## END
358
359#### var ref TO array with arbitrary subscripts
360shopt -s eval_unsafe_arith compat_array
361
362f() {
363 local val=$(echo "${!1}")
364 if test "$val" = y; then
365 echo "works: $1"
366 fi
367}
368# Warmup: nice plain array reference
369a=(x y)
370f 'a[1]'
371#
372# Not allowed:
373# no brace expansion
374f 'a[{1,0}]' # operand expected
375# no process substitution (but see command substitution below!)
376f 'a[<(echo x)]' # operand expected
377# TODO word splitting seems interesting
378aa="1 0"
379f 'a[$aa]' # 1 0: syntax error in expression (error token is "0")
380# no filename globbing
381f 'a[b*]' # operand expected
382f 'a[1"]' # bad substitution
383#
384# Allowed: most everything else in section 3.5 "Shell Expansions".
385# shell parameter expansion
386b=1
387f 'a[$b]'
388f 'a[${c:-1}]'
389# (... and presumably most of the other features there)
390# command substitution, yikes!
391f 'a[$(echo 1)]'
392# arithmetic expansion
393f 'a[$(( 3 - 2 ))]'
394
395# All of these are undocumented and probably shouldn't exist,
396# though it's always possible some will turn up in the wild and
397# we'll end up implementing them.
398
399## STDOUT:
400works: a[1]
401works: a[$b]
402works: a[${c:-1}]
403works: a[$(echo 1)]
404works: a[$(( 3 - 2 ))]
405## END
406
407#### Bizarre tilde expansion in array index
408a=(x y)
409PWD=1
410ref='a[~+]'
411echo ${!ref}
412## status: 1
413
414# Bash 4.4 had a bug, which was fixed in Bash 5.0.
415#
416# ## BUG bash status: 0
417# ## BUG bash STDOUT:
418# y
419# ## END
420
421#### Indirect expansion TO fancy expansion features bash disallows
422
423check_indir() {
424 result="${!1}"
425 desugared_result=$(eval 'echo "${'"$1"'}"')
426 [ "$2" = "$desugared_result" ] || { echo "$1 $desugared_result"; }
427}
428x=y
429y=a
430a=(x y)
431declare -A aa
432aa=([k]=r [l]=s)
433# malformed array indexing
434check_indir "a[0"
435check_indir "aa[k"
436# double indirection
437check_indir "!x" a
438check_indir "!a[0]" y
439# apparently everything else in the manual under "Shell Parameter Expansion"
440check_indir "x:-foo" y
441check_indir "x:=foo" y
442check_indir "x:?oops" y
443check_indir "x:+yy" yy
444check_indir "x:0" y
445check_indir "x:0:1" y
446check_indir "!a@" "a aa"
447# (!a[@] is elsewhere)
448check_indir "#x" 1
449check_indir "x#y"
450check_indir "x/y/foo" foo
451check_indir "x@Q" y
452echo done
453## status: 0
454## stdout: done
455
456#### Bad var ref
457a='bad var name'
458echo ref ${!a}
459echo status=$?
460
461## STDOUT:
462status=1
463## END
464
465#### Bad var ref 2
466b='/' # really bad
467echo ref ${!b}
468echo status=$?
469## STDOUT:
470status=1
471## END
472
473#### ${!OPTIND} (used by bash completion
474set -- a b c
475echo ${!OPTIND}
476f() {
477 local OPTIND=1
478 echo ${!OPTIND}
479 local OPTIND=2
480 echo ${!OPTIND}
481}
482f x y z
483## STDOUT:
484a
485x
486y
487## END
488
489#### var ref doesn't need cycle detection
490x=y
491y=x
492echo cycle=${!x}
493
494typeset -n a=b
495typeset -n b=a
496echo cycle=${a}
497## status: 1
498## STDOUT:
499cycle=x
500## END
501## OK bash status: 0
502## OK bash STDOUT:
503cycle=x
504cycle=
505## END
506
507#### Var Ref Code Injection $(tee PWNED)
508
509typeset -a a
510a=(42)
511
512x='a[$(echo 0 | tee PWNED)]'
513
514echo ${!x}
515
516if test -f PWNED; then
517 echo PWNED
518 cat PWNED
519else
520 echo NOPE
521fi
522
523## status: 1
524## STDOUT:
525## END
526
527## BUG bash status: 0
528## BUG bash STDOUT:
52942
530PWNED
5310
532## END
533
534#### ${!array_ref:-set} and ${!array_ref:=assign}
535
536ref='a[@]'
537a=('' '' '')
538
539echo "==== check ===="
540
541argv.py "${!ref:-set}"
542argv.py "${a[@]:-set}"
543
544echo "==== assign ===="
545
546argv.py "${!ref:=assign}"
547argv.py "${!ref}"
548a=('' '' '') # revert the state in case it is modified
549
550argv.py "${a[@]:=assign}"
551argv.py "${a[@]}"
552
553## STDOUT:
554==== check ====
555['', '', '']
556['', '', '']
557==== assign ====
558['', '', '']
559['', '', '']
560['', '', '']
561['', '', '']
562## END
563
564#### Array indirect expansion with suffix operators
565
566declare -A ref=(['dummy']=v1)
567function test-suffixes {
568 echo "==== $1 ===="
569 ref['dummy']=$1
570 argv.py "${!ref[@]:2}"
571 argv.py "${!ref[@]:1:2}"
572 argv.py "${!ref[@]:-empty}"
573 argv.py "${!ref[@]:+set}"
574 argv.py "${!ref[@]:=assign}"
575}
576
577v1=value
578test-suffixes v1
579echo "v1=$v1"
580
581v2=
582test-suffixes v2
583echo "v2=$v2"
584
585a1=()
586test-suffixes a1
587argv.py "${a1[@]}"
588
589a2=(element)
590test-suffixes 'a2[0]'
591argv.py "${a2[@]}"
592
593a3=(1 2 3)
594test-suffixes 'a3[@]'
595argv.py "${a3[@]}"
596
597## STDOUT:
598==== v1 ====
599['lue']
600['al']
601['value']
602['set']
603['value']
604v1=value
605==== v2 ====
606['']
607['']
608['empty']
609['']
610['assign']
611v2=assign
612==== a1 ====
613['']
614['']
615['empty']
616['']
617['assign']
618['assign']
619==== a2[0] ====
620['ement']
621['le']
622['element']
623['set']
624['element']
625['element']
626==== a3[@] ====
627['3']
628['2', '3']
629['1', '2', '3']
630['set']
631['1', '2', '3']
632['1', '2', '3']
633## END
634
635#### Array indirect expansion with replacements
636
637declare -A ref=(['dummy']=v1)
638function test-rep {
639 echo "==== $1 ===="
640 ref['dummy']=$1
641 argv.py "${!ref[@]#?}"
642 argv.py "${!ref[@]%?}"
643 argv.py "${!ref[@]//[a-f]}"
644 argv.py "${!ref[@]//[a-f]/x}"
645}
646
647v1=value
648test-rep v1
649
650v2=
651test-rep v2
652
653a1=()
654test-rep a1
655
656a2=(element)
657test-rep 'a2[0]'
658
659a3=(1 2 3)
660test-rep 'a3[@]'
661
662## STDOUT:
663==== v1 ====
664['alue']
665['valu']
666['vlu']
667['vxlux']
668==== v2 ====
669['']
670['']
671['']
672['']
673==== a1 ====
674['']
675['']
676['']
677['']
678==== a2[0] ====
679['lement']
680['elemen']
681['lmnt']
682['xlxmxnt']
683==== a3[@] ====
684['', '', '']
685['', '', '']
686['1', '2', '3']
687['1', '2', '3']
688## END
689
690#### Array indirect expansion with @? conversion
691
692declare -A ref=(['dummy']=v1)
693function test-op0 {
694 echo "==== $1 ===="
695 ref['dummy']=$1
696 argv.py "${!ref[@]@Q}"
697 argv.py "${!ref[@]@P}"
698 argv.py "${!ref[@]@a}"
699}
700
701v1=value
702test-op0 v1
703
704v2=
705test-op0 v2
706
707a1=()
708test-op0 a1
709
710a2=(element)
711test-op0 'a2[0]'
712
713a3=(1 2 3)
714test-op0 'a3[@]'
715
716## STDOUT:
717==== v1 ====
718['value']
719['value']
720['']
721==== v2 ====
722["''"]
723['']
724['']
725==== a1 ====
726['']
727['']
728['a']
729==== a2[0] ====
730['element']
731['element']
732['a']
733==== a3[@] ====
734['1', '2', '3']
735['1', '2', '3']
736['a', 'a', 'a']
737## END
738
739# Bash 4.4 had a bug in the section "==== a3[@] ====":
740#
741# ==== a3[@] ====
742# []
743# []
744# []
745
746## OK bash STDOUT:
747==== v1 ====
748["'value'"]
749['value']
750['']
751==== v2 ====
752["''"]
753['']
754['']
755==== a1 ====
756['']
757['']
758['a']
759==== a2[0] ====
760["'element'"]
761['element']
762['a']
763==== a3[@] ====
764["'1'", "'2'", "'3'"]
765['1', '2', '3']
766['a', 'a', 'a']
767## END