| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Arrays decay upon assignment (without splicing) and equality. This will not |
| 4 | # be true in Oil -- arrays will be first class. |
| 5 | |
| 6 | ### Assignment Causes Array Decay |
| 7 | set -- x y z |
| 8 | #argv "[$@]" # NOT DECAYED here. |
| 9 | var="[$@]" |
| 10 | argv.py "$var" |
| 11 | # stdout: ['[x y z]'] |
| 12 | |
| 13 | ### User arrays decay |
| 14 | declare -a a b |
| 15 | a=(x y z) |
| 16 | b="${a[@]}" # this collapses to a string |
| 17 | c=("${a[@]}") # this preserves the array |
| 18 | c[1]=YYY # mutate a copy -- doesn't affect the original |
| 19 | argv.py "${a[@]}" "${b[@]}" "${c[@]}" |
| 20 | # stdout: ['x', 'y', 'z', 'x y z', 'x', 'YYY', 'z'] |
| 21 | |
| 22 | ### $a gives first element of array |
| 23 | a=(1 '2 3') |
| 24 | echo $a |
| 25 | # stdout: 1 |
| 26 |