1 #
2 # Arrays decay upon assignment (without splicing) and equality. This will not
3 # be true in Oil -- arrays will be first class.
4
5 #### Assignment Causes Array Decay
6 set -- x y z
7 argv.py "[$@]"
8 var="[$@]"
9 argv.py "$var"
10 ## STDOUT:
11 ['[x', 'y', 'z]']
12 ['[x y z]']
13 ## END
14
15 #### Array Decay with IFS
16 IFS=x
17 set -- x y z
18 var="[$@]"
19 argv.py "$var"
20 ## stdout: ['[x y z]']
21
22 #### User arrays decay
23 declare -a a b
24 a=(x y z)
25 b="${a[@]}" # this collapses to a string
26 c=("${a[@]}") # this preserves the array
27 c[1]=YYY # mutate a copy -- doesn't affect the original
28 argv.py "${a[@]}"
29 argv.py "${b}"
30 argv.py "${c[@]}"
31 ## STDOUT:
32 ['x', 'y', 'z']
33 ['x y z']
34 ['x', 'YYY', 'z']
35 ## END
36
37 #### $array is not valid in OSH, is ${array[0]} in ksh/bash
38 a=(1 '2 3')
39 echo $a
40 ## STDOUT:
41 1
42 ## END
43 ## OK osh status: 1
44 ## OK osh stdout-json: ""
45
46 #### ${array} is not valid in OSH, is ${array[0]} in ksh/bash
47 a=(1 '2 3')
48 echo ${a}
49 ## STDOUT:
50 1
51 ## END
52 ## OK osh status: 1
53 ## OK osh stdout-json: ""
54
55 #### Assign to array index without initialization
56 a[5]=5
57 a[6]=6
58 echo "${a[@]}" ${#a[@]}
59 ## stdout: 5 6 2
60
61 #### a[40] grows array
62 a=(1 2 3)
63 a[1]=5
64 a[40]=30 # out of order
65 a[10]=20
66 echo "${a[@]}" "${#a[@]}" # length is 1
67 ## stdout: 1 5 3 20 30 5
68
69 #### array decays to string when comparing with [[ a = b ]]
70 a=('1 2' '3 4')
71 s='1 2 3 4' # length 2, length 4
72 echo ${#a[@]} ${#s}
73 [[ "${a[@]}" = "$s" ]] && echo EQUAL
74 ## STDOUT:
75 2 7
76 EQUAL
77 ## END
78
79 #### ++ on a whole array increments the first element (disallowed in OSH)
80 a=(1 10)
81 (( a++ )) # doesn't make sense
82 echo "${a[@]}"
83 ## stdout: 2 10
84 ## OK osh status: 1
85 ## OK osh stdout-json: ""
86
87 #### Apply vectorized operations on ${a[*]}
88 a=('-x-' 'y-y' '-z-')
89
90 # This does the prefix stripping FIRST, and then it joins.
91 argv.py "${a[*]#-}"
92 ## STDOUT:
93 ['x- y-y z-']
94 ## END
95 ## N-I mksh status: 1
96 ## N-I mksh stdout-json: ""