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