1 #!/usr/bin/env bash
2
3 # TODO: Need a SETUP section.
4
5 #### SETUP
6 a=(1 '2 3')
7
8 #### "${a[@]}" and "${a[*]}"
9 a=(1 '2 3')
10 argv.py "${a[@]}" "${a[*]}"
11 ## stdout: ['1', '2 3', '1 2 3']
12
13 #### ${a[@]} and ${a[*]}
14 a=(1 '2 3')
15 argv.py ${a[@]} ${a[*]}
16 ## stdout: ['1', '2', '3', '1', '2', '3']
17
18 #### 4 ways to interpolate empty array
19 argv.py 1 "${a[@]}" 2 ${a[@]} 3 "${a[*]}" 4 ${a[*]} 5
20 ## stdout: ['1', '2', '3', '', '4', '5']
21
22 #### empty array
23 empty=()
24 argv.py "${empty[@]}"
25 ## stdout: []
26
27 #### Empty array with :-
28 empty=()
29 argv.py ${empty[@]:-not one} "${empty[@]:-not one}"
30 ## stdout: ['not', 'one', 'not one']
31
32 #### nounset with empty array (design bug, makes it hard to use arrays)
33 # http://lists.gnu.org/archive/html/help-bash/2017-09/msg00005.html
34 # NOTE: This used to be a bug in bash 4.3, but is fixed in bash 4.4.
35 set -o nounset
36 empty=()
37 argv.py "${empty[@]}"
38 echo status=$?
39 ## STDOUT:
40 []
41 status=0
42 ## END
43 ## BUG mksh stdout-json: ""
44 ## BUG mksh status: 1
45
46 #### local array
47 # mksh support local variables, but not local arrays, oddly.
48 f() {
49 local a=(1 '2 3')
50 argv.py "${a[0]}"
51 }
52 f
53 ## stdout: ['1']
54 ## status: 0
55 ## BUG mksh status: 1
56 ## BUG mksh stdout-json: ""
57
58 #### Command with with word splitting in array
59 array=('1 2' $(echo '3 4'))
60 argv.py "${array[@]}"
61 ## stdout: ['1 2', '3', '4']
62
63 #### space before ( in array initialization
64 # NOTE: mksh accepts this, but bash doesn't
65 a= (1 '2 3')
66 echo $a
67 ## status: 2
68 ## OK mksh status: 0
69 ## OK mksh stdout: 1
70
71 #### array over multiple lines
72 a=(
73 1
74 '2 3'
75 )
76 argv.py "${a[@]}"
77 ## stdout: ['1', '2 3']
78 ## status: 0
79
80 #### array with invalid token
81 a=(
82 1
83 &
84 '2 3'
85 )
86 argv.py "${a[@]}"
87 ## status: 2
88 ## OK mksh status: 1
89
90 #### array with empty string
91 empty=('')
92 argv.py "${empty[@]}"
93 ## stdout: ['']
94
95 #### Retrieve index
96 a=(1 '2 3')
97 argv.py "${a[1]}"
98 ## stdout: ['2 3']
99
100 #### Retrieve out of bounds index
101 a=(1 '2 3')
102 argv.py "${a[3]}"
103 ## stdout: ['']
104
105 #### Negative index
106 a=(1 '2 3')
107 argv.py "${a[-1]}" "${a[-2]}" "${a[-5]}" # last one out of bounds
108 ## stdout: ['2 3', '1', '']
109 ## N-I mksh stdout: ['', '', '']
110
111 #### Retrieve index that is a variable
112 a=(1 '2 3')
113 i=1
114 argv.py "${a[$i]}"
115 ## stdout: ['2 3']
116
117 #### Retrieve index that is a variable without $
118 a=(1 '2 3')
119 i=5
120 argv.py "${a[i-4]}"
121 ## stdout: ['2 3']
122
123 #### Retrieve index that is a command sub
124 a=(1 '2 3')
125 argv.py "${a[$(echo 1)]}"
126 ## stdout: ['2 3']
127
128 #### Retrieve array indices with ${!a}
129 a=(1 '2 3')
130 argv.py "${!a[@]}"
131 ## stdout: ['0', '1']
132
133 #### Retrieve sparse array indices with ${!a}
134 a=()
135 (( a[99]=1 ))
136 argv.py "${!a[@]}"
137 ## STDOUT:
138 ['99']
139 ## END
140
141 #### ${!a[1]} is named ref in bash
142 # mksh ignores it
143 foo=bar
144 a=('1 2' foo '2 3')
145 argv.py "${!a[1]}"
146 ## status: 0
147 ## stdout: ['bar']
148 ## N-I mksh stdout: ['a[1]']
149
150 #### ${!a} on array
151 # bash gives empty string?
152 # mksh gives the name of the variable with !. Very weird.
153 a=(1 '2 3')
154 argv.py "${!a}"
155 ## stdout: ['']
156 ## OK mksh stdout: ['a']
157
158 #### All elements unquoted
159 a=(1 '2 3')
160 argv.py ${a[@]}
161 ## stdout: ['1', '2', '3']
162
163 #### All elements quoted
164 a=(1 '2 3')
165 argv.py "${a[@]}"
166 ## stdout: ['1', '2 3']
167
168 #### $*
169 a=(1 '2 3')
170 argv.py ${a[*]}
171 ## stdout: ['1', '2', '3']
172
173 #### "$*"
174 a=(1 '2 3')
175 argv.py "${a[*]}"
176 ## stdout: ['1 2 3']
177
178 #### Interpolate array into array
179 a=(1 '2 3')
180 a=(0 "${a[@]}" '4 5')
181 argv.py "${a[@]}"
182 ## stdout: ['0', '1', '2 3', '4 5']
183
184 #### Exporting array doesn't do anything, not even first element
185 # bash parses, but doesn't execute.
186 # mksh gives syntax error -- parses differently with 'export'
187 # osh no longer parses this statically.
188 export PYTHONPATH=(a b c)
189 export PYTHONPATH=a # NOTE: in bash, this doesn't work afterward!
190 printenv.py PYTHONPATH
191 ## stdout: None
192 ## OK mksh stdout-json: ""
193 ## OK mksh status: 1
194 ## OK osh stdout-json: ""
195 ## OK osh status: 2
196
197 #### Arrays can't be used as env bindings
198 # Hm bash it treats it as a string!
199 A=a B=(b b) printenv.py A B
200 ## status: 2
201 ## stdout-json: ""
202 ## OK bash stdout-json: "a\n(b b)\n"
203 ## OK bash status: 0
204 ## OK mksh status: 1
205
206 #### Set element
207 a=(1 '2 3')
208 a[0]=9
209 argv.py "${a[@]}"
210 ## stdout: ['9', '2 3']
211
212 #### Set element with var ref
213 a=(1 '2 3')
214 i=0
215 a[$i]=9
216 argv.py "${a[@]}"
217 ## stdout: ['9', '2 3']
218
219 #### Set element with array ref
220 # This makes parsing a little more complex. Anything can be inside [],
221 # including other [].
222 a=(1 '2 3')
223 i=(0 1)
224 a[${i[1]}]=9
225 argv.py "${a[@]}"
226 ## stdout: ['1', '9']
227
228 #### Set array item to array
229 a=(1 2)
230 a[0]=(3 4)
231 echo "status=$?"
232 ## stdout: status=1
233 ## status: 0
234 ## N-I mksh stdout-json: ""
235 ## N-I mksh status: 1
236
237 #### Slice of array with [@]
238 # mksh doesn't support this syntax! It's a bash extension.
239 a=(1 2 3)
240 argv.py "${a[@]:1:2}"
241 ## stdout: ['2', '3']
242 ## N-I mksh status: 1
243 ## N-I mksh stdout-json: ""
244
245 #### Negative slice
246 # mksh doesn't support this syntax! It's a bash extension.
247 # NOTE: for some reason -2) has to be in parens? Ah that's because it
248 # conflicts with :-! That's silly. You can also add a space.
249 a=(1 2 3)
250 argv.py "${a[@]:(-2):1}"
251 ## stdout: ['2']
252 ## N-I mksh status: 1
253 ## N-I mksh stdout-json: ""
254
255 #### Slice with arithmetic
256 a=(1 2 3)
257 i=5
258 argv.py "${a[@]:i-4:2}"
259 ## stdout: ['2', '3']
260 ## N-I mksh status: 1
261 ## N-I mksh stdout-json: ""
262
263 #### Number of elements
264 a=(1 '2 3')
265 echo "${#a[@]}" ${#a[@]} # bug fix: also test without quotes
266 ## stdout: 2 2
267
268 #### Length of an element
269 a=(1 '2 3')
270 echo "${#a[1]}"
271 ## stdout: 3
272
273 #### Iteration
274 a=(1 '2 3')
275 for v in "${a[@]}"; do
276 echo $v
277 done
278 ## stdout-json: "1\n2 3\n"
279
280 #### glob within array yields separate elements
281 touch _tmp/y.Y _tmp/yy.Y
282 a=(_tmp/*.Y)
283 argv.py "${a[@]}"
284 ## stdout: ['_tmp/y.Y', '_tmp/yy.Y']
285
286 #### declare array and then append
287 declare -a array
288 array+=(a)
289 array+=(b c)
290 argv.py "${array[@]}"
291 ## stdout: ['a', 'b', 'c']
292
293 #### Array syntax in wrong place
294 ls foo=(1 2)
295 ## status: 2
296 ## OK mksh status: 1
297
298 #### Single array with :-
299 # bash does EMPTY ELISION here, unless it's double quoted. mksh has
300 # more sane behavior. OSH is better.
301 single=('')
302 argv.py ${single[@]:-none} x "${single[@]:-none}"
303 ## OK osh stdout: ['x', '']
304 ## OK bash stdout: ['none', 'x', '']
305 ## OK mksh stdout: ['none', 'x', 'none']
306
307 #### Stripping a whole array unquoted
308 # Problem: it joins it first.
309 files=('foo.c' 'sp ace.h' 'bar.c')
310 argv.py ${files[@]%.c}
311 ## status: 0
312 ## stdout: ['foo', 'sp', 'ace.h', 'bar']
313 ## N-I mksh status: 1
314 ## N-I mksh stdout-json: ""
315
316 #### Stripping a whole array quoted
317 files=('foo.c' 'sp ace.h' 'bar.c')
318 argv.py "${files[@]%.c}"
319 ## status: 0
320 ## stdout: ['foo', 'sp ace.h', 'bar']
321 ## N-I mksh status: 1
322 ## N-I mksh stdout-json: ""
323
324 #### Multiple subscripts not allowed
325 # NOTE: bash 4.3 had a bug where it ignored the bad subscript, but now it is
326 # fixed.
327 a=('123' '456')
328 argv.py "${a[0]}" "${a[0][0]}"
329 ## stdout-json: ""
330 ## status: 2
331 ## OK bash/mksh status: 1
332
333 #### Length op, index op, then transform op is not allowed
334 a=('123' '456')
335 echo "${#a[0]}" "${#a[0]/1/xxx}"
336 ## stdout-json: ""
337 ## status: 2
338 ## OK bash/mksh status: 1
339
340 #### Array subscript not allowed on string
341 s='abc'
342 echo ${s[@]}
343 ## BUG bash/mksh status: 0
344 ## BUG bash/mksh stdout: abc
345 ## status: 1
346
347 #### Create a "user" array out of the argv array
348 set -- 'a b' 'c'
349 array1=('x y' 'z')
350 array2=("$@")
351 argv.py "${array1[@]}" "${array2[@]}"
352 ## stdout: ['x y', 'z', 'a b', 'c']
353
354 #### Tilde expansion within array
355 HOME=/home/bob
356 a=(~/src ~/git)
357 echo "${a[@]}"
358 ## stdout: /home/bob/src /home/bob/git
359
360 #### Brace Expansion within Array
361 a=(-{a,b} {c,d}-)
362 echo "${a[@]}"
363 ## stdout: -a -b c- d-
364
365 #### array default
366 default=('1 2' '3')
367 argv.py "${undef[@]:-${default[@]}}"
368 ## stdout: ['1 2', '3']
369
370 #### Singleton Array Copy and Assign. Can't index string with int.
371 a=( '12 3' )
372 b=( "${a[@]}" )
373 c="${a[@]}" # This decays it to a string
374 d=$a # This decays it to a string
375 echo ${#a[0]} ${#b[0]}
376 echo ${#a[@]} ${#b[@]}
377 # osh is intentionally stricter about arrays, and these fail.
378 echo ${#c[0]} ${#d[0]}
379 echo ${#c[@]} ${#d[@]}
380 ## status: 1
381 ## STDOUT:
382 4 4
383 1 1
384 ## END
385 ## OK bash/mksh status: 0
386 ## OK bash/mksh STDOUT:
387 4 4
388 1 1
389 4 4
390 1 1
391 ## END
392
393 #### declare -a / local -a is empty array
394 declare -a myarray
395 argv.py "${myarray[@]}"
396 myarray+=('x')
397 argv.py "${myarray[@]}"
398
399 f() {
400 local -a myarray
401 argv.py "${myarray[@]}"
402 myarray+=('x')
403 argv.py "${myarray[@]}"
404 }
405 f
406 ## STDOUT:
407 []
408 ['x']
409 []
410 ['x']
411 ## END
412
413 #### Create sparse array
414 a=()
415 (( a[99]=1 )) # osh doesn't parse index assignment outside arithmetic yet
416 echo len=${#a[@]}
417 argv.py "${a[@]}"
418 echo "unset=${a[33]}"
419 echo len-of-unset=${#a[33]}
420 ## STDOUT:
421 len=1
422 ['1']
423 unset=
424 len-of-unset=0
425 ## END
426
427 #### Create sparse array implicitly
428 (( a[99]=1 ))
429 echo len=${#a[@]}
430 argv.py "${a[@]}"
431 echo "unset=${a[33]}"
432 echo len-of-unset=${#a[33]}
433 ## STDOUT:
434 len=1
435 ['1']
436 unset=
437 len-of-unset=0
438 ## END
439
440 #### Append sparse arrays
441 a=()
442 (( a[99]=1 ))
443 b=()
444 (( b[33]=2 ))
445 (( b[66]=3 ))
446 a+=( "${b[@]}" )
447 argv.py "${a[@]}"
448 argv.py "${a[99]}" "${a[100]}" "${a[101]}"
449 ## STDOUT:
450 ['1', '2', '3']
451 ['1', '2', '3']
452 ## END
453
454 #### Slice of sparse array with [@]
455 # mksh doesn't support this syntax! It's a bash extension.
456 (( a[33]=1 ))
457 (( a[66]=2 ))
458 (( a[99]=2 ))
459 argv.py "${a[@]:15:2}"
460 ## stdout: ['1', '2']
461 ## N-I mksh status: 1
462 ## N-I mksh stdout-json: ""
463
464 #### Using an array itself as the index
465 # TODO: Fix OSH crash.
466 a[a]=42
467 a[a]=99
468 argv "${a[@]}" "${a[0]}" "${a[42]}" "${a[99]}"
469 ## STDOUT:
470 ['42', '99', '42', '99', '']
471 ## END
472