| 1 | ## compare_shells: bash-4.4
|
| 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}
|
| 10 | a=b
|
| 11 | b=c
|
| 12 | echo ref ${!a} ${a}
|
| 13 | ## stdout: ref c b
|
| 14 |
|
| 15 | #### ${!ref-default}
|
| 16 | ref=x
|
| 17 | echo x=${!ref-default}
|
| 18 |
|
| 19 | x=''
|
| 20 | echo x=${!ref-default}
|
| 21 |
|
| 22 | x=foo
|
| 23 | echo x=${!ref-default}
|
| 24 |
|
| 25 | ## STDOUT:
|
| 26 | x=default
|
| 27 | x=
|
| 28 | x=foo
|
| 29 | ## END
|
| 30 |
|
| 31 | #### ${!undef:-}
|
| 32 | # bash gives empty string, but I feel like this could be an error
|
| 33 | echo undef=${!undef-'default'}
|
| 34 | echo undef=${!undef}
|
| 35 |
|
| 36 | set -u
|
| 37 | echo NOUNSET
|
| 38 | echo undef=${!undef-'default'}
|
| 39 | echo undef=${!undef}
|
| 40 |
|
| 41 | ## status: 1
|
| 42 | ## STDOUT:
|
| 43 | undef=default
|
| 44 | undef=
|
| 45 | NOUNSET
|
| 46 | undef=default
|
| 47 | ## END
|
| 48 |
|
| 49 | #### comparison to ${!array[@]} keys (similar SYNTAX)
|
| 50 |
|
| 51 | declare -a a=(x y)
|
| 52 | argv.py "${!a[@]}"
|
| 53 | echo a_keys=$?
|
| 54 |
|
| 55 | argv.py "${!a}" # missing [] is equivalent to ${!a[0]} ?
|
| 56 | echo a_nobrackets=$?
|
| 57 |
|
| 58 | echo ---
|
| 59 | declare -A A=([A]=a [B]=b)
|
| 60 |
|
| 61 | argv.py ${!A[@]}
|
| 62 | echo A_keys=$?
|
| 63 |
|
| 64 | argv.py "${!A}" # missing [] is equivalent to ${!A[0]} ?
|
| 65 | echo A_nobrackets=$?
|
| 66 |
|
| 67 | ## STDOUT:
|
| 68 | ['0', '1']
|
| 69 | a_keys=0
|
| 70 | ['']
|
| 71 | a_nobrackets=0
|
| 72 | ---
|
| 73 | ['A', 'B']
|
| 74 | A_keys=0
|
| 75 | ['']
|
| 76 | A_nobrackets=0
|
| 77 | ## END
|
| 78 |
|
| 79 | #### ${!a[@]-'default'} is illegal
|
| 80 |
|
| 81 | # bash disallows this when a is an array. We make it an error because [@]
|
| 82 | # implies it's an array.
|
| 83 |
|
| 84 | argv.py "${!a[@]-default}"
|
| 85 | echo status=$?
|
| 86 |
|
| 87 | a=(x y z)
|
| 88 | argv.py "${!a[@]-default}"
|
| 89 | echo status=$?
|
| 90 | ## status: 1
|
| 91 | ## STDOUT:
|
| 92 | ## END
|
| 93 | ## BUG bash status: 0
|
| 94 | ## BUG bash STDOUT:
|
| 95 | ['default']
|
| 96 | status=0
|
| 97 | status=1
|
| 98 | ## END
|
| 99 |
|
| 100 |
|
| 101 | #### var ref to $@ with @
|
| 102 | set -- one two
|
| 103 | ref='@'
|
| 104 | echo ref=${!ref}
|
| 105 | ## STDOUT:
|
| 106 | ref=one two
|
| 107 | ## END
|
| 108 |
|
| 109 | #### var ref to $1 and $2 with 1 and 2
|
| 110 | set -- one two
|
| 111 | ref1='1'
|
| 112 | echo ref1=${!ref1}
|
| 113 | ref2='2'
|
| 114 | echo ref2=${!ref2}
|
| 115 |
|
| 116 | ## STDOUT:
|
| 117 | ref1=one
|
| 118 | ref2=two
|
| 119 | ## END
|
| 120 |
|
| 121 | #### var ref: 1, @, *
|
| 122 | set -- x y
|
| 123 | ref=1; argv.py "${!ref}"
|
| 124 | ref=@; argv.py "${!ref}"
|
| 125 | ref=*; argv.py "${!ref}" # maybe_decay_array bug?
|
| 126 |
|
| 127 | ## STDOUT:
|
| 128 | ['x']
|
| 129 | ['x', 'y']
|
| 130 | ['x y']
|
| 131 | ## END
|
| 132 |
|
| 133 | #### var ref to special var BASH_SOURCE
|
| 134 | ref='LINENO'
|
| 135 | echo lineno=${!ref}
|
| 136 | ## STDOUT:
|
| 137 | lineno=2
|
| 138 | ## END
|
| 139 |
|
| 140 | #### var ref to $? with '?'
|
| 141 | myfunc() {
|
| 142 | local ref=$1
|
| 143 | echo ${!ref}
|
| 144 | }
|
| 145 | myfunc FUNCNAME
|
| 146 | myfunc '?'
|
| 147 | ## STDOUT:
|
| 148 | myfunc
|
| 149 | 0
|
| 150 | ## END
|
| 151 |
|
| 152 |
|
| 153 | #### Var ref, then assignment with ${ := }
|
| 154 | z=zz
|
| 155 | zz=
|
| 156 | echo ${!z:=foo}
|
| 157 | echo ${!z:=bar}
|
| 158 | ## STDOUT:
|
| 159 | foo
|
| 160 | foo
|
| 161 | ## END
|
| 162 |
|
| 163 | #### Var ref, then error with ${ ? }
|
| 164 | w=ww
|
| 165 | ww=
|
| 166 | echo ${!w:?'my message'}
|
| 167 | echo done
|
| 168 | ## status: 1
|
| 169 | ## STDOUT:
|
| 170 | ## END
|
| 171 |
|
| 172 | #### Indirect expansion, THEN suffix operators
|
| 173 |
|
| 174 | check_eq() {
|
| 175 | [ "$1" = "$2" ] || { echo "$1 vs $2"; }
|
| 176 | }
|
| 177 | check_expand() {
|
| 178 | val=$(eval "echo \"$1\"")
|
| 179 | [ "$val" = "$2" ] || { echo "$1 -> expected $2, got $val"; }
|
| 180 | }
|
| 181 | check_err() {
|
| 182 | e="$1"
|
| 183 | msg=$(eval "$e" 2>&1) && echo "bad success: $e"
|
| 184 | if test -n "$2"; then
|
| 185 | if [[ "$msg" != $2 ]]; then
|
| 186 | echo "Expected error: $e"
|
| 187 | echo "Got error : $msg"
|
| 188 | fi
|
| 189 | fi
|
| 190 | }
|
| 191 | # Nearly everything in manual section 3.5.3 "Shell Parameter Expansion"
|
| 192 | # is allowed after a !-indirection.
|
| 193 | #
|
| 194 | # Not allowed: any further prefix syntax.
|
| 195 | x=xx; xx=aaabcc
|
| 196 | xd=x
|
| 197 | check_err '${!!xd}'
|
| 198 | check_err '${!!x*}'
|
| 199 | a=(asdf x)
|
| 200 | check_err '${!!a[*]}'
|
| 201 | check_err '${!#x}'
|
| 202 | check_err '${!#a[@]}'
|
| 203 | # And an array reference binds tighter in the syntax, so goes first;
|
| 204 | # there's no way to spell "indirection, then array reference".
|
| 205 | check_expand '${!a[1]}' xx
|
| 206 | b=(aoeu a)
|
| 207 | check_expand '${!b[1]}' asdf # i.e. like !(b[1]), not (!b)[1]
|
| 208 | #
|
| 209 | # Allowed: apparently everything else.
|
| 210 | y=yy; yy=
|
| 211 | check_expand '${!y:-foo}' foo
|
| 212 | check_expand '${!x:-foo}' aaabcc
|
| 213 |
|
| 214 | check_expand '${!x:?oops}' aaabcc
|
| 215 |
|
| 216 | check_expand '${!y:+foo}' ''
|
| 217 | check_expand '${!x:+foo}' foo
|
| 218 |
|
| 219 | check_expand '${!x:2}' abcc
|
| 220 | check_expand '${!x:2:2}' ab
|
| 221 |
|
| 222 | check_expand '${!x#*a}' aabcc
|
| 223 | check_expand '${!x%%c*}' aaab
|
| 224 | check_expand '${!x/a*b/d}' dcc
|
| 225 |
|
| 226 | # ^ operator not fully implemented in OSH
|
| 227 | #check_expand '${!x^a}' Aaabcc
|
| 228 |
|
| 229 | p=pp; pp='\$ '
|
| 230 | check_expand '${!p@P}' '$ '
|
| 231 | echo ok
|
| 232 | ## stdout: ok
|
| 233 |
|
| 234 | #### var ref OF array var -- silent a[0] decay
|
| 235 | declare -a a=(ale bean)
|
| 236 | echo first=${!a}
|
| 237 |
|
| 238 | ale=zzz
|
| 239 | echo first=${!a}
|
| 240 |
|
| 241 | ## status: 0
|
| 242 | ## STDOUT:
|
| 243 | first=
|
| 244 | first=zzz
|
| 245 | ## END
|
| 246 |
|
| 247 | #### array ref
|
| 248 |
|
| 249 | declare -a array=(ale bean)
|
| 250 | ref='array[0]'
|
| 251 | echo ${!ref}
|
| 252 | ## status: 0
|
| 253 | ## STDOUT:
|
| 254 | ale
|
| 255 | ## END
|
| 256 |
|
| 257 | #### array ref with strict_array
|
| 258 | shopt -s strict_array
|
| 259 |
|
| 260 | declare -a array=(ale bean)
|
| 261 | ref='array'
|
| 262 | echo ${!ref}
|
| 263 | ## status: 1
|
| 264 | ## stdout-json: ""
|
| 265 | ## N-I bash status: 0
|
| 266 | ## N-I bash STDOUT:
|
| 267 | ale
|
| 268 | ## END
|
| 269 |
|
| 270 | #### var ref TO array var
|
| 271 | shopt -s compat_array
|
| 272 |
|
| 273 | declare -a array=(ale bean)
|
| 274 |
|
| 275 | ref='array' # when compat_array is on, this is like array[0]
|
| 276 | ref_AT='array[@]'
|
| 277 |
|
| 278 | echo ${!ref}
|
| 279 | echo ${!ref_AT}
|
| 280 |
|
| 281 | ## STDOUT:
|
| 282 | ale
|
| 283 | ale bean
|
| 284 | ## END
|
| 285 |
|
| 286 | #### var ref TO array var, with subscripts
|
| 287 | f() {
|
| 288 | argv.py "${!1}"
|
| 289 | }
|
| 290 | f 'nonexistent[0]'
|
| 291 | array=(x y z)
|
| 292 | f 'array[0]'
|
| 293 | f 'array[1+1]'
|
| 294 | f 'array[@]'
|
| 295 | f 'array[*]'
|
| 296 | # Also associative arrays.
|
| 297 | ## STDOUT:
|
| 298 | ['']
|
| 299 | ['x']
|
| 300 | ['z']
|
| 301 | ['x', 'y', 'z']
|
| 302 | ['x y z']
|
| 303 | ## END
|
| 304 |
|
| 305 | #### var ref TO assoc array a[key]
|
| 306 | shopt -s compat_array
|
| 307 |
|
| 308 | declare -A assoc=([ale]=bean [corn]=dip)
|
| 309 | ref=assoc
|
| 310 | #ref_AT='assoc[@]'
|
| 311 |
|
| 312 | # UNQUOTED doesn't work with Oil's parser
|
| 313 | #ref_SUB='assoc[ale]'
|
| 314 | ref_SUB='assoc["ale"]'
|
| 315 |
|
| 316 | ref_SUB_QUOTED='assoc["al"e]'
|
| 317 |
|
| 318 | ref_SUB_BAD='assoc["bad"]'
|
| 319 |
|
| 320 | echo ref=${!ref} # compat_array: assoc is equivalent to assoc[0]
|
| 321 | #echo ref_AT=${!ref_AT}
|
| 322 | echo ref_SUB=${!ref_SUB}
|
| 323 | echo ref_SUB_QUOTED=${!ref_SUB_QUOTED}
|
| 324 | echo ref_SUB_BAD=${!ref_SUB_BAD}
|
| 325 |
|
| 326 | ## STDOUT:
|
| 327 | ref=
|
| 328 | ref_SUB=bean
|
| 329 | ref_SUB_QUOTED=bean
|
| 330 | ref_SUB_BAD=
|
| 331 | ## END
|
| 332 |
|
| 333 | #### var ref TO array with arbitrary subscripts
|
| 334 | shopt -s eval_unsafe_arith compat_array
|
| 335 |
|
| 336 | f() {
|
| 337 | local val=$(echo "${!1}")
|
| 338 | if test "$val" = y; then
|
| 339 | echo "works: $1"
|
| 340 | fi
|
| 341 | }
|
| 342 | # Warmup: nice plain array reference
|
| 343 | a=(x y)
|
| 344 | f 'a[1]'
|
| 345 | #
|
| 346 | # Not allowed:
|
| 347 | # no brace expansion
|
| 348 | f 'a[{1,0}]' # operand expected
|
| 349 | # no process substitution (but see command substitution below!)
|
| 350 | f 'a[<(echo x)]' # operand expected
|
| 351 | # TODO word splitting seems interesting
|
| 352 | aa="1 0"
|
| 353 | f 'a[$aa]' # 1 0: syntax error in expression (error token is "0")
|
| 354 | # no filename globbing
|
| 355 | f 'a[b*]' # operand expected
|
| 356 | f 'a[1"]' # bad substitution
|
| 357 | #
|
| 358 | # Allowed: most everything else in section 3.5 "Shell Expansions".
|
| 359 | # shell parameter expansion
|
| 360 | b=1
|
| 361 | f 'a[$b]'
|
| 362 | f 'a[${c:-1}]'
|
| 363 | # (... and presumably most of the other features there)
|
| 364 | # command substitution, yikes!
|
| 365 | f 'a[$(echo 1)]'
|
| 366 | # arithmetic expansion
|
| 367 | f 'a[$(( 3 - 2 ))]'
|
| 368 |
|
| 369 | # All of these are undocumented and probably shouldn't exist,
|
| 370 | # though it's always possible some will turn up in the wild and
|
| 371 | # we'll end up implementing them.
|
| 372 |
|
| 373 | ## STDOUT:
|
| 374 | works: a[1]
|
| 375 | works: a[$b]
|
| 376 | works: a[${c:-1}]
|
| 377 | works: a[$(echo 1)]
|
| 378 | works: a[$(( 3 - 2 ))]
|
| 379 | ## END
|
| 380 |
|
| 381 | #### Bizarre tilde expansion in array index
|
| 382 | a=(x y)
|
| 383 | PWD=1
|
| 384 | ref='a[~+]'
|
| 385 | echo ${!ref}
|
| 386 | ## status: 1
|
| 387 | ## BUG bash status: 0
|
| 388 | ## BUG bash STDOUT:
|
| 389 | y
|
| 390 | ## END
|
| 391 |
|
| 392 | #### Indirect expansion TO fancy expansion features bash disallows
|
| 393 |
|
| 394 | check_indir() {
|
| 395 | result="${!1}"
|
| 396 | desugared_result=$(eval 'echo "${'"$1"'}"')
|
| 397 | [ "$2" = "$desugared_result" ] || { echo "$1 $desugared_result"; }
|
| 398 | }
|
| 399 | x=y
|
| 400 | y=a
|
| 401 | a=(x y)
|
| 402 | declare -A aa
|
| 403 | aa=([k]=r [l]=s)
|
| 404 | # malformed array indexing
|
| 405 | check_indir "a[0"
|
| 406 | check_indir "aa[k"
|
| 407 | # double indirection
|
| 408 | check_indir "!x" a
|
| 409 | check_indir "!a[0]" y
|
| 410 | # apparently everything else in the manual under "Shell Parameter Expansion"
|
| 411 | check_indir "x:-foo" y
|
| 412 | check_indir "x:=foo" y
|
| 413 | check_indir "x:?oops" y
|
| 414 | check_indir "x:+yy" yy
|
| 415 | check_indir "x:0" y
|
| 416 | check_indir "x:0:1" y
|
| 417 | check_indir "!a@" "a aa"
|
| 418 | # (!a[@] is elsewhere)
|
| 419 | check_indir "#x" 1
|
| 420 | check_indir "x#y"
|
| 421 | check_indir "x/y/foo" foo
|
| 422 | check_indir "x@Q" "'y'"
|
| 423 | echo done
|
| 424 | ## status: 1
|
| 425 | ## stdout-json: ""
|
| 426 | ## OK bash status: 0
|
| 427 | ## OK bash stdout: done
|
| 428 |
|
| 429 | #### Bad var ref
|
| 430 | a='bad var name'
|
| 431 | echo ref ${!a}
|
| 432 | echo status=$?
|
| 433 |
|
| 434 | ## STDOUT:
|
| 435 | status=1
|
| 436 | ## END
|
| 437 | ## OK osh stdout-json: ""
|
| 438 | ## OK osh status: 1
|
| 439 |
|
| 440 | #### Bad var ref 2
|
| 441 | b='/' # really bad
|
| 442 | echo ref ${!b}
|
| 443 | echo status=$?
|
| 444 | ## STDOUT:
|
| 445 | status=1
|
| 446 | ## END
|
| 447 | ## OK osh stdout-json: ""
|
| 448 | ## OK osh status: 1
|
| 449 |
|
| 450 | #### ${!OPTIND} (used by bash completion
|
| 451 | set -- a b c
|
| 452 | echo ${!OPTIND}
|
| 453 | f() {
|
| 454 | local OPTIND=1
|
| 455 | echo ${!OPTIND}
|
| 456 | local OPTIND=2
|
| 457 | echo ${!OPTIND}
|
| 458 | }
|
| 459 | f x y z
|
| 460 | ## STDOUT:
|
| 461 | a
|
| 462 | x
|
| 463 | y
|
| 464 | ## END
|
| 465 |
|
| 466 | #### var ref doesn't need cycle detection
|
| 467 | x=y
|
| 468 | y=x
|
| 469 | echo cycle=${!x}
|
| 470 |
|
| 471 | typeset -n a=b
|
| 472 | typeset -n b=a
|
| 473 | echo cycle=${a}
|
| 474 | ## status: 1
|
| 475 | ## STDOUT:
|
| 476 | cycle=x
|
| 477 | ## END
|
| 478 | ## OK bash status: 0
|
| 479 | ## OK bash STDOUT:
|
| 480 | cycle=x
|
| 481 | cycle=
|
| 482 | ## END
|
| 483 |
|
| 484 | #### Var Ref Code Injection $(tee PWNED)
|
| 485 |
|
| 486 | typeset -a a
|
| 487 | a=(42)
|
| 488 |
|
| 489 | x='a[$(echo 0 | tee PWNED)]'
|
| 490 |
|
| 491 | echo ${!x}
|
| 492 |
|
| 493 | if test -f PWNED; then
|
| 494 | echo PWNED
|
| 495 | cat PWNED
|
| 496 | else
|
| 497 | echo NOPE
|
| 498 | fi
|
| 499 |
|
| 500 | ## status: 1
|
| 501 | ## STDOUT:
|
| 502 | ## END
|
| 503 |
|
| 504 | ## BUG bash status: 0
|
| 505 | ## BUG bash STDOUT:
|
| 506 | 42
|
| 507 | PWNED
|
| 508 | 0
|
| 509 | ## END
|