1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# this shows the expansions an array subscript undergoes before being run
# through the arithmetic evaluator
b=(0 1 2 3)
# array subscripts undergo variable expansion
a=2
echo ${b[$a]}
# array subscripts undergo command substitution
echo ${b[$(echo 2)]}
c='1+1'
d='1-+1'
# array subscripts are expanded and the expanded value is treated as an
# expression
echo ${b[$c]}
echo ${b[c]}
echo ${b[$d]}
echo ${b[d]}
# array subscripts undergo parameter expansion
set -- 1 2 3
echo ${b[$1]}
# array subscripts undergo tilde expansion
HOME=2
echo ${b[~]}
# array subscripts undergo word splitting -- bug in bash versions through 4.3
x='b[$d]'
IFS=-
echo $((x))
IFS=$' \t\n'
set -- 1 + 2
x='d'
IFS=-
echo $((x))
IFS=$' \t\n'
# start of quoting tests; make sure that subscript is treated as double
# quoted (inhibits word splitting) but that double quotes are silently
# discarded through quote removal
echo $(( $@ ))
echo "$(( $@ ))"
echo $(( "$x" ))
echo $(( "x" ))
unset a foo bar
a=(zero one two three four five six seven eight nine ten)
echo ${a[0]}
echo ${a["0"]}
foo=1
echo ${a[$foo]}
echo ${a["$foo"]}
echo ${a[foo]}
echo ${a["foo"]}
bar=2
echo ${a[" $bar "]}
echo ${a[" bar "]}
# tilde expansion is performed by array subscript expansion but not by posix
# style arithmetic expansion
HOME=2
echo $(( ~ ))
|