1 ## compare_shells: bash mksh
2 ## oils_failures_allowed: 2
3
4 # Arrays decay upon assignment (without splicing) and equality.
5
6 #### Assignment Causes Array Decay
7 set -- x y z
8 argv.py "[$@]"
9 var="[$@]"
10 argv.py "$var"
11 ## STDOUT:
12 ['[x', 'y', 'z]']
13 ['[x y z]']
14 ## END
15
16 #### Array Decay with IFS
17 IFS=x
18 set -- x y z
19 var="[$@]"
20 argv.py "$var"
21 ## stdout: ['[x y z]']
22
23 #### User arrays decay
24 declare -a a b
25 a=(x y z)
26 b="${a[@]}" # this collapses to a string
27 c=("${a[@]}") # this preserves the array
28 c[1]=YYY # mutate a copy -- doesn't affect the original
29 argv.py "${a[@]}"
30 argv.py "${b}"
31 argv.py "${c[@]}"
32 ## STDOUT:
33 ['x', 'y', 'z']
34 ['x y z']
35 ['x', 'YYY', 'z']
36 ## END
37
38 #### strict_array: $array is not valid in OSH, is ${array[0]} in ksh/bash
39 shopt -s strict_array
40
41 a=(1 '2 3')
42 echo $a
43 ## STDOUT:
44 1
45 ## END
46 ## OK osh status: 1
47 ## OK osh stdout-json: ""
48
49 #### strict_array: ${array} is not valid in OSH, is ${array[0]} in ksh/bash
50 shopt -s strict_array
51
52 a=(1 '2 3')
53 echo ${a}
54 ## STDOUT:
55 1
56 ## END
57 ## OK osh status: 1
58 ## OK osh stdout-json: ""
59
60 #### Assign to array index without initialization
61 a[5]=5
62 a[6]=6
63 echo "${a[@]}" ${#a[@]}
64 ## stdout: 5 6 2
65
66 #### a[40] grows array
67 a=(1 2 3)
68 a[1]=5
69 a[40]=30 # out of order
70 a[10]=20
71 echo "${a[@]}" "${#a[@]}" # length is 1
72 ## stdout: 1 5 3 20 30 5
73
74 #### array decays to string when comparing with [[ a = b ]]
75 a=('1 2' '3 4')
76 s='1 2 3 4' # length 2, length 4
77 echo ${#a[@]} ${#s}
78 [[ "${a[@]}" = "$s" ]] && echo EQUAL
79 ## STDOUT:
80 2 7
81 EQUAL
82 ## END
83
84 #### ++ on a whole array increments the first element (disallowed with strict_array)
85 shopt -s strict_array
86
87 a=(1 10)
88 (( a++ )) # doesn't make sense
89 echo "${a[@]}"
90 ## stdout: 2 10
91 ## OK osh status: 1
92 ## OK osh stdout-json: ""
93
94 #### Apply vectorized operations on ${a[*]}
95 a=('-x-' 'y-y' '-z-')
96
97 # This does the prefix stripping FIRST, and then it joins.
98 argv.py "${a[*]#-}"
99 ## STDOUT:
100 ['x- y-y z-']
101 ## END
102 ## N-I mksh status: 1
103 ## N-I mksh stdout-json: ""
104
105 #### value.BashArray internal representation - Indexed
106
107 case $SH in mksh) exit ;; esac
108
109 z=()
110 declare -a | grep z=
111
112 z+=(b c)
113 declare -a | grep z=
114
115 # z[5]= finds the index, or puts it in SORTED order I think
116 z[5]=d
117 declare -a | grep z=
118
119 z[1]=ZZZ
120 declare -a | grep z=
121
122 # Adds after last index
123 z+=(f g)
124 declare -a | grep z=
125
126 # This is the equivalent of z[0]+=mystr
127 z+=-mystr
128 declare -a | grep z=
129
130 z[1]+=-append
131 declare -a | grep z=
132
133 argv.py keys "${!z[@]}" # 0 1 5 6 7
134 argv.py values "${z[@]}"
135
136 # can't do this conversion
137 declare -A z
138 declare -A | grep z=
139
140 echo status=$?
141
142 ## STDOUT:
143 declare -a z=()
144 declare -a z=([0]="b" [1]="c")
145 declare -a z=([0]="b" [1]="c" [5]="d")
146 declare -a z=([0]="b" [1]="ZZZ" [5]="d")
147 declare -a z=([0]="b" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
148 declare -a z=([0]="b-mystr" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
149 declare -a z=([0]="b-mystr" [1]="ZZZ-append" [5]="d" [6]="f" [7]="g")
150 ['keys', '0', '1', '5', '6', '7']
151 ['values', 'b-mystr', 'ZZZ-append', 'd', 'f', 'g']
152 status=1
153 ## END
154
155 ## N-I mksh STDOUT:
156 ## END
157
158 #### value.BashArray internal representation - Assoc (ordering is a problem)
159
160 case $SH in mksh) exit ;; esac
161
162 declare -A A=([k]=v)
163 declare -A | grep A=
164
165 argv.py keys "${!A[@]}"
166 argv.py values "${A[@]}"
167
168 exit
169
170 # Huh this actually works, we don't support it
171 # Hm the order here is all messed up, in bash 5.2
172 A+=([k2]=v2 [0]=foo [9]=9 [9999]=9999)
173 declare -A | grep A=
174
175 A+=-append
176 declare -A | grep A=
177
178 argv.py keys "${!A[@]}"
179 argv.py values "${A[@]}"
180
181 ## STDOUT:
182 declare -A A=([k]="v" )
183 ['keys', 'k']
184 ['values', 'v']
185 ## END
186
187 ## N-I mksh STDOUT:
188 ## END