OILS / spec / var-ref.test.sh View on Github | oilshell.org

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