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