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 is disallowed
151 # bash gives empty string because it's like a[0]
152 # mksh gives the name of the variable with !. Very weird.
153 a=(1 '2 3')
154 argv.py "${!a}"
155 ## stdout-json: ""
156 ## status: 1
157 ## BUG bash stdout: ['']
158 ## BUG bash status: 0
159 ## BUG mksh stdout: ['a']
160 ## BUG mksh status: 0
161
162 #### All elements unquoted
163 a=(1 '2 3')
164 argv.py ${a[@]}
165 ## stdout: ['1', '2', '3']
166
167 #### All elements quoted
168 a=(1 '2 3')
169 argv.py "${a[@]}"
170 ## stdout: ['1', '2 3']
171
172 #### $*
173 a=(1 '2 3')
174 argv.py ${a[*]}
175 ## stdout: ['1', '2', '3']
176
177 #### "$*"
178 a=(1 '2 3')
179 argv.py "${a[*]}"
180 ## stdout: ['1 2 3']
181
182 #### Interpolate array into array
183 a=(1 '2 3')
184 a=(0 "${a[@]}" '4 5')
185 argv.py "${a[@]}"
186 ## stdout: ['0', '1', '2 3', '4 5']
187
188 #### Exporting array doesn't do anything, not even first element
189 # bash parses, but doesn't execute.
190 # mksh gives syntax error -- parses differently with 'export'
191 # osh no longer parses this statically.
192 export PYTHONPATH=(a b c)
193 export PYTHONPATH=a # NOTE: in bash, this doesn't work afterward!
194 printenv.py PYTHONPATH
195 ## stdout-json: ""
196 ## status: 1
197 ## OK bash stdout: None
198 ## OK bash status: 0
199
200 #### Arrays can't be used as env bindings
201 # Hm bash it treats it as a string!
202 A=a B=(b b) printenv.py A B
203 ## status: 2
204 ## stdout-json: ""
205 ## OK bash stdout-json: "a\n(b b)\n"
206 ## OK bash status: 0
207 ## OK mksh status: 1
208
209 #### Set element
210 a=(1 '2 3')
211 a[0]=9
212 argv.py "${a[@]}"
213 ## stdout: ['9', '2 3']
214
215 #### Set element with var ref
216 a=(1 '2 3')
217 i=0
218 a[$i]=9
219 argv.py "${a[@]}"
220 ## stdout: ['9', '2 3']
221
222 #### Set element with array ref
223 # This makes parsing a little more complex. Anything can be inside [],
224 # including other [].
225 a=(1 '2 3')
226 i=(0 1)
227 a[${i[1]}]=9
228 argv.py "${a[@]}"
229 ## stdout: ['1', '9']
230
231 #### Set array item to array
232 a=(1 2)
233 a[0]=(3 4)
234 echo "status=$?"
235 ## stdout-json: ""
236 ## status: 2
237 ## N-I mksh status: 1
238 ## BUG bash stdout: status=1
239 ## BUG bash status: 0
240
241 #### Slice of array with [@]
242 # mksh doesn't support this syntax! It's a bash extension.
243 a=(1 2 3)
244 argv.py "${a[@]:1:2}"
245 ## stdout: ['2', '3']
246 ## N-I mksh status: 1
247 ## N-I mksh stdout-json: ""
248
249 #### Negative slice begin
250 # mksh doesn't support this syntax! It's a bash extension.
251 # NOTE: for some reason -2) has to be in parens? Ah that's because it
252 # conflicts with :-! That's silly. You can also add a space.
253 a=(1 2 3 4 5)
254 argv.py "${a[@]:(-4)}"
255 ## stdout: ['2', '3', '4', '5']
256 ## N-I mksh status: 1
257 ## N-I mksh stdout-json: ""
258
259 #### Negative slice length
260 a=(1 2 3 4 5)
261 argv.py "${a[@]: 1: -3}"
262 ## status: 1
263 ## stdout-json: ""
264
265 #### Slice with arithmetic
266 a=(1 2 3)
267 i=5
268 argv.py "${a[@]:i-4:2}"
269 ## stdout: ['2', '3']
270 ## N-I mksh status: 1
271 ## N-I mksh stdout-json: ""
272
273 #### Number of elements
274 a=(1 '2 3')
275 echo "${#a[@]}" ${#a[@]} # bug fix: also test without quotes
276 ## stdout: 2 2
277
278 #### Length of an element
279 a=(1 '2 3')
280 echo "${#a[1]}"
281 ## stdout: 3
282
283 #### Iteration
284 a=(1 '2 3')
285 for v in "${a[@]}"; do
286 echo $v
287 done
288 ## stdout-json: "1\n2 3\n"
289
290 #### glob within array yields separate elements
291 touch _tmp/y.Y _tmp/yy.Y
292 a=(_tmp/*.Y)
293 argv.py "${a[@]}"
294 ## stdout: ['_tmp/y.Y', '_tmp/yy.Y']
295
296 #### declare array and then append
297 declare -a array
298 array+=(a)
299 array+=(b c)
300 argv.py "${array[@]}"
301 ## stdout: ['a', 'b', 'c']
302
303 #### Array syntax in wrong place
304 ls foo=(1 2)
305 ## status: 1
306 ## OK bash status: 2
307
308 #### Single array with :-
309 # bash does EMPTY ELISION here, unless it's double quoted. mksh has
310 # more sane behavior. OSH is better.
311 single=('')
312 argv.py ${single[@]:-none} x "${single[@]:-none}"
313 ## OK osh stdout: ['x', '']
314 ## OK bash stdout: ['none', 'x', '']
315 ## OK mksh stdout: ['none', 'x', 'none']
316
317 #### Stripping a whole array unquoted
318 # Problem: it joins it first.
319 files=('foo.c' 'sp ace.h' 'bar.c')
320 argv.py ${files[@]%.c}
321 ## status: 0
322 ## stdout: ['foo', 'sp', 'ace.h', 'bar']
323 ## N-I mksh status: 1
324 ## N-I mksh stdout-json: ""
325
326 #### Stripping a whole array quoted
327 files=('foo.c' 'sp ace.h' 'bar.c')
328 argv.py "${files[@]%.c}"
329 ## status: 0
330 ## stdout: ['foo', 'sp ace.h', 'bar']
331 ## N-I mksh status: 1
332 ## N-I mksh stdout-json: ""
333
334 #### Multiple subscripts not allowed
335 # NOTE: bash 4.3 had a bug where it ignored the bad subscript, but now it is
336 # fixed.
337 a=('123' '456')
338 argv.py "${a[0]}" "${a[0][0]}"
339 ## stdout-json: ""
340 ## status: 2
341 ## OK bash/mksh status: 1
342
343 #### Length op, index op, then transform op is not allowed
344 a=('123' '456')
345 echo "${#a[0]}" "${#a[0]/1/xxx}"
346 ## stdout-json: ""
347 ## status: 2
348 ## OK bash/mksh status: 1
349
350 #### Array subscript not allowed on string
351 s='abc'
352 echo ${s[@]}
353 ## BUG bash/mksh status: 0
354 ## BUG bash/mksh stdout: abc
355 ## status: 1
356
357 #### Create a "user" array out of the argv array
358 set -- 'a b' 'c'
359 array1=('x y' 'z')
360 array2=("$@")
361 argv.py "${array1[@]}" "${array2[@]}"
362 ## stdout: ['x y', 'z', 'a b', 'c']
363
364 #### Tilde expansion within array
365 HOME=/home/bob
366 a=(~/src ~/git)
367 echo "${a[@]}"
368 ## stdout: /home/bob/src /home/bob/git
369
370 #### Brace Expansion within Array
371 a=(-{a,b} {c,d}-)
372 echo "${a[@]}"
373 ## stdout: -a -b c- d-
374
375 #### array default
376 default=('1 2' '3')
377 argv.py "${undef[@]:-${default[@]}}"
378 ## stdout: ['1 2', '3']
379
380 #### Singleton Array Copy and Assign. OSH can't index strings with ints
381 a=( '12 3' )
382 b=( "${a[@]}" )
383 c="${a[@]}" # This decays it to a string
384 d=${a[*]} # This decays it to a string
385 echo ${#a[0]} ${#b[0]}
386 echo ${#a[@]} ${#b[@]}
387
388 # osh is intentionally stricter, and these fail.
389 echo ${#c[0]} ${#d[0]}
390 echo ${#c[@]} ${#d[@]}
391
392 ## status: 1
393 ## STDOUT:
394 4 4
395 1 1
396 ## END
397 ## OK bash/mksh status: 0
398 ## OK bash/mksh STDOUT:
399 4 4
400 1 1
401 4 4
402 1 1
403 ## END
404
405 #### declare -a / local -a is empty array
406 declare -a myarray
407 argv.py "${myarray[@]}"
408 myarray+=('x')
409 argv.py "${myarray[@]}"
410
411 f() {
412 local -a myarray
413 argv.py "${myarray[@]}"
414 myarray+=('x')
415 argv.py "${myarray[@]}"
416 }
417 f
418 ## STDOUT:
419 []
420 ['x']
421 []
422 ['x']
423 ## END
424
425 #### Create sparse array
426 a=()
427 (( a[99]=1 )) # osh doesn't parse index assignment outside arithmetic yet
428 echo len=${#a[@]}
429 argv.py "${a[@]}"
430 echo "unset=${a[33]}"
431 echo len-of-unset=${#a[33]}
432 ## STDOUT:
433 len=1
434 ['1']
435 unset=
436 len-of-unset=0
437 ## END
438
439 #### Create sparse array implicitly
440 (( a[99]=1 ))
441 echo len=${#a[@]}
442 argv.py "${a[@]}"
443 echo "unset=${a[33]}"
444 echo len-of-unset=${#a[33]}
445 ## STDOUT:
446 len=1
447 ['1']
448 unset=
449 len-of-unset=0
450 ## END
451
452 #### Append sparse arrays
453 a=()
454 (( a[99]=1 ))
455 b=()
456 (( b[33]=2 ))
457 (( b[66]=3 ))
458 a+=( "${b[@]}" )
459 argv.py "${a[@]}"
460 argv.py "${a[99]}" "${a[100]}" "${a[101]}"
461 ## STDOUT:
462 ['1', '2', '3']
463 ['1', '2', '3']
464 ## END
465
466 #### Slice of sparse array with [@]
467 # mksh doesn't support this syntax! It's a bash extension.
468 (( a[33]=1 ))
469 (( a[66]=2 ))
470 (( a[99]=2 ))
471 argv.py "${a[@]:15:2}"
472 ## stdout: ['1', '2']
473 ## N-I mksh status: 1
474 ## N-I mksh stdout-json: ""
475
476 #### Using an array itself as the index on LHS
477 shopt -u strict-arith
478 a[a]=42
479 a[a]=99
480 argv "${a[@]}" "${a[0]}" "${a[42]}" "${a[99]}"
481
482 ## status: 1
483 ## stdout-json: ""
484
485 ## BUG bash/mksh status: 0
486 ## BUG bash/mksh STDOUT:
487 ['42', '99', '42', '99', '']
488 ## END
489
490 #### Using an array itself as the index on RHS
491 shopt -u strict-arith
492 a=(1 2 3)
493 (( x = a[a] ))
494 echo $x
495 ## status: 1
496 ## stdout-json: ""
497 ## BUG bash/mksh status: 0
498 ## BUG bash/mksh STDOUT:
499 2
500 ## END
501
502 #### a[$x$y] on LHS and RHS
503 x=1
504 y=2
505 a[$x$y]=foo
506
507 # not allowed by OSH parsing
508 #echo ${a[$x$y]}
509
510 echo ${a[12]}
511 echo ${#a[@]}
512
513 ## STDOUT:
514 foo
515 1
516 ## END