1 |
## compare_shells: bash mksh
|
2 |
## oils_failures_allowed: 3
|
3 |
|
4 |
|
5 |
|
6 |
# http://lists.gnu.org/archive/html/help-bash/2017-09/msg00005.html
|
7 |
|
8 |
set -o nounset
|
9 |
empty=()
|
10 |
argv.py "${empty[@]}"
|
11 |
echo status=$?
|
12 |
## STDOUT:
|
13 |
[]
|
14 |
status=0
|
15 |
## END
|
16 |
## BUG mksh stdout-json: ""
|
17 |
## BUG mksh status: 1
|
18 |
|
19 |
|
20 |
# mksh support local variables, but not local arrays, oddly.
|
21 |
f() {
|
22 |
local a=(1 '2 3')
|
23 |
argv.py "${a[0]}"
|
24 |
}
|
25 |
f
|
26 |
## stdout: ['1']
|
27 |
## status: 0
|
28 |
## BUG mksh status: 1
|
29 |
## BUG mksh stdout-json: ""
|
30 |
|
31 |
|
32 |
array=('1 2' $(echo '3 4'))
|
33 |
argv.py "${array[@]}"
|
34 |
## stdout: ['1 2', '3', '4']
|
35 |
|
36 |
|
37 |
# NOTE: mksh accepts this, but bash doesn't
|
38 |
a= (1 '2 3')
|
39 |
echo $a
|
40 |
## status: 2
|
41 |
## OK mksh status: 0
|
42 |
## OK mksh stdout: 1
|
43 |
|
44 |
|
45 |
a=(
|
46 |
1
|
47 |
'2 3'
|
48 |
)
|
49 |
argv.py "${a[@]}"
|
50 |
## stdout: ['1', '2 3']
|
51 |
## status: 0
|
52 |
|
53 |
|
54 |
a=(
|
55 |
1
|
56 |
&
|
57 |
'2 3'
|
58 |
)
|
59 |
argv.py "${a[@]}"
|
60 |
## status: 2
|
61 |
## OK mksh status: 1
|
62 |
|
63 |
|
64 |
empty=('')
|
65 |
argv.py "${empty[@]}"
|
66 |
## stdout: ['']
|
67 |
|
68 |
|
69 |
a=(1 '2 3')
|
70 |
argv.py "${a[1]}"
|
71 |
## stdout: ['2 3']
|
72 |
|
73 |
|
74 |
a=(1 '2 3')
|
75 |
argv.py "${a[3]}"
|
76 |
## stdout: ['']
|
77 |
|
78 |
|
79 |
a=(1 '2 3')
|
80 |
argv.py "${a[-1]}" "${a[-2]}" "${a[-5]}" # last one out of bounds
|
81 |
## stdout: ['2 3', '1', '']
|
82 |
## N-I mksh stdout: ['', '', '']
|
83 |
|
84 |
|
85 |
a=(0 1 2 3 4)
|
86 |
unset a[1]
|
87 |
unset a[4]
|
88 |
echo "${a[@]}"
|
89 |
echo -1 ${a[-1]}
|
90 |
echo -2 ${a[-2]}
|
91 |
echo -3 ${a[-3]}
|
92 |
echo -4 ${a[-4]}
|
93 |
echo -5 ${a[-5]}
|
94 |
|
95 |
a[-1]+=0 # append 0 on the end
|
96 |
echo ${a[@]}
|
97 |
(( a[-1] += 42 ))
|
98 |
echo ${a[@]}
|
99 |
|
100 |
## STDOUT:
|
101 |
0 2 3
|
102 |
-1 3
|
103 |
-2 2
|
104 |
-3
|
105 |
-4 0
|
106 |
-5
|
107 |
0 2 30
|
108 |
0 2 72
|
109 |
## END
|
110 |
## BUG mksh STDOUT:
|
111 |
0 2 3
|
112 |
-1
|
113 |
-2
|
114 |
-3
|
115 |
-4
|
116 |
-5
|
117 |
0 2 3 0
|
118 |
0 2 3 42
|
119 |
## END
|
120 |
|
121 |
|
122 |
a=(0 1)
|
123 |
unset 'a[-1]' # remove last element
|
124 |
a+=(2 3)
|
125 |
echo ${a[0]} $((a[0]))
|
126 |
echo ${a[1]} $((a[1]))
|
127 |
echo ${a[2]} $((a[2]))
|
128 |
echo ${a[3]} $((a[3]))
|
129 |
## STDOUT:
|
130 |
0 0
|
131 |
2 2
|
132 |
3 3
|
133 |
0
|
134 |
## END
|
135 |
## BUG mksh STDOUT:
|
136 |
0 0
|
137 |
1 1
|
138 |
2 2
|
139 |
3 3
|
140 |
## END
|
141 |
|
142 |
|
143 |
a=(0 1 2 3)
|
144 |
unset a[-1]
|
145 |
echo len=${#a[@]}
|
146 |
unset a[-1]
|
147 |
echo len=${#a[@]}
|
148 |
## STDOUT:
|
149 |
len=3
|
150 |
len=2
|
151 |
## END
|
152 |
## BUG mksh STDOUT:
|
153 |
len=4
|
154 |
len=4
|
155 |
## END
|
156 |
|
157 |
|
158 |
a=(1 '2 3')
|
159 |
i=1
|
160 |
argv.py "${a[$i]}"
|
161 |
## stdout: ['2 3']
|
162 |
|
163 |
|
164 |
a=(1 '2 3')
|
165 |
i=5
|
166 |
argv.py "${a[i-4]}"
|
167 |
## stdout: ['2 3']
|
168 |
|
169 |
|
170 |
a=(1 '2 3')
|
171 |
argv.py "${a[$(echo 1)]}"
|
172 |
## stdout: ['2 3']
|
173 |
|
174 |
|
175 |
a=(1 '2 3')
|
176 |
argv.py "${!a[@]}"
|
177 |
## stdout: ['0', '1']
|
178 |
|
179 |
|
180 |
a=()
|
181 |
(( a[99]=1 ))
|
182 |
argv.py "${!a[@]}"
|
183 |
## STDOUT:
|
184 |
['99']
|
185 |
## END
|
186 |
|
187 |
|
188 |
# mksh ignores it
|
189 |
foo=bar
|
190 |
a=('1 2' foo '2 3')
|
191 |
argv.py "${!a[1]}"
|
192 |
## status: 0
|
193 |
## stdout: ['bar']
|
194 |
## N-I mksh stdout: ['a[1]']
|
195 |
|
196 |
|
197 |
|
198 |
# bash gives empty string because it's like a[0]
|
199 |
# mksh gives the name of the variable with !. Very weird.
|
200 |
|
201 |
a=(1 '2 3')
|
202 |
argv.py "${!a}"
|
203 |
|
204 |
## stdout: ['']
|
205 |
## status: 0
|
206 |
## BUG mksh stdout: ['a']
|
207 |
## BUG mksh status: 0
|
208 |
|
209 |
|
210 |
a=(1 '2 3')
|
211 |
argv.py ${a[@]}
|
212 |
## stdout: ['1', '2', '3']
|
213 |
|
214 |
|
215 |
a=(1 '2 3')
|
216 |
argv.py "${a[@]}"
|
217 |
## stdout: ['1', '2 3']
|
218 |
|
219 |
|
220 |
a=(1 '2 3')
|
221 |
argv.py ${a[*]}
|
222 |
## stdout: ['1', '2', '3']
|
223 |
|
224 |
|
225 |
a=(1 '2 3')
|
226 |
argv.py "${a[*]}"
|
227 |
## stdout: ['1 2 3']
|
228 |
|
229 |
|
230 |
a=(1 '2 3')
|
231 |
a=(0 "${a[@]}" '4 5')
|
232 |
argv.py "${a[@]}"
|
233 |
## stdout: ['0', '1', '2 3', '4 5']
|
234 |
|
235 |
|
236 |
# bash parses, but doesn't execute.
|
237 |
# mksh gives syntax error -- parses differently with 'export'
|
238 |
# osh no longer parses this statically.
|
239 |
|
240 |
export PYTHONPATH
|
241 |
|
242 |
PYTHONPATH=mystr # NOTE: in bash, this doesn't work afterward!
|
243 |
printenv.py PYTHONPATH
|
244 |
|
245 |
PYTHONPATH=(myarray)
|
246 |
printenv.py PYTHONPATH
|
247 |
|
248 |
PYTHONPATH=(a b c)
|
249 |
printenv.py PYTHONPATH
|
250 |
|
251 |
## status: 0
|
252 |
## STDOUT:
|
253 |
mystr
|
254 |
None
|
255 |
None
|
256 |
## END
|
257 |
|
258 |
|
259 |
|
260 |
shopt -s strict_array
|
261 |
|
262 |
export PYTHONPATH
|
263 |
PYTHONPATH=(a b c)
|
264 |
printenv.py PYTHONPATH
|
265 |
|
266 |
## status: 1
|
267 |
## STDOUT:
|
268 |
## END
|
269 |
|
270 |
## N-I bash/mksh status: 0
|
271 |
## N-I bash/mksh STDOUT:
|
272 |
None
|
273 |
## END
|
274 |
|
275 |
|
276 |
# Hm bash it treats it as a string!
|
277 |
A=a B=(b b) printenv.py A B
|
278 |
## status: 2
|
279 |
## stdout-json: ""
|
280 |
## OK bash stdout-json: "a\n(b b)\n"
|
281 |
## OK bash status: 0
|
282 |
## OK mksh status: 1
|
283 |
|
284 |
|
285 |
a=(1 '2 3')
|
286 |
a[0]=9
|
287 |
argv.py "${a[@]}"
|
288 |
## stdout: ['9', '2 3']
|
289 |
|
290 |
|
291 |
a=(1 '2 3')
|
292 |
i=0
|
293 |
a[$i]=9
|
294 |
argv.py "${a[@]}"
|
295 |
## stdout: ['9', '2 3']
|
296 |
|
297 |
|
298 |
# This makes parsing a little more complex. Anything can be inside [],
|
299 |
# including other [].
|
300 |
a=(1 '2 3')
|
301 |
i=(0 1)
|
302 |
a[${i[1]}]=9
|
303 |
argv.py "${a[@]}"
|
304 |
## stdout: ['1', '9']
|
305 |
|
306 |
|
307 |
a=(1 2)
|
308 |
a[0]=(3 4)
|
309 |
echo "status=$?"
|
310 |
## stdout-json: ""
|
311 |
## status: 2
|
312 |
## N-I mksh status: 1
|
313 |
## BUG bash stdout: status=1
|
314 |
## BUG bash status: 0
|
315 |
|
316 |
|
317 |
# mksh doesn't support this syntax! It's a bash extension.
|
318 |
a=(1 2 3)
|
319 |
argv.py "${a[@]:1:2}"
|
320 |
## stdout: ['2', '3']
|
321 |
## N-I mksh status: 1
|
322 |
## N-I mksh stdout-json: ""
|
323 |
|
324 |
|
325 |
# mksh doesn't support this syntax! It's a bash extension.
|
326 |
# NOTE: for some reason -2) has to be in parens? Ah that's because it
|
327 |
# conflicts with :-! That's silly. You can also add a space.
|
328 |
a=(1 2 3 4 5)
|
329 |
argv.py "${a[@]:(-4)}"
|
330 |
## stdout: ['2', '3', '4', '5']
|
331 |
## N-I mksh status: 1
|
332 |
## N-I mksh stdout-json: ""
|
333 |
|
334 |
|
335 |
a=(1 2 3 4 5)
|
336 |
argv.py "${a[@]: 1: -3}"
|
337 |
## status: 1
|
338 |
## stdout-json: ""
|
339 |
|
340 |
|
341 |
a=(1 2 3)
|
342 |
i=5
|
343 |
argv.py "${a[@]:i-4:2}"
|
344 |
## stdout: ['2', '3']
|
345 |
## N-I mksh status: 1
|
346 |
## N-I mksh stdout-json: ""
|
347 |
|
348 |
|
349 |
a=(1 '2 3')
|
350 |
echo "${#a[@]}" ${#a[@]} # bug fix: also test without quotes
|
351 |
## stdout: 2 2
|
352 |
|
353 |
|
354 |
a=(1 '2 3')
|
355 |
echo "${#a[1]}"
|
356 |
## stdout: 3
|
357 |
|
358 |
|
359 |
a=(1 '2 3')
|
360 |
for v in "${a[@]}"; do
|
361 |
echo $v
|
362 |
done
|
363 |
## stdout-json: "1\n2 3\n"
|
364 |
|
365 |
|
366 |
touch _tmp/y.Y _tmp/yy.Y
|
367 |
a=(_tmp/*.Y)
|
368 |
argv.py "${a[@]}"
|
369 |
## stdout: ['_tmp/y.Y', '_tmp/yy.Y']
|
370 |
|
371 |
|
372 |
declare -a array
|
373 |
array+=(a)
|
374 |
array+=(b c)
|
375 |
argv.py "${array[@]}"
|
376 |
## stdout: ['a', 'b', 'c']
|
377 |
|
378 |
|
379 |
ls foo=(1 2)
|
380 |
## status: 1
|
381 |
## OK bash status: 2
|
382 |
|
383 |
|
384 |
|
385 |
# 2024-06 - bash 5.2 and mksh now match, bash 4.4 differed.
|
386 |
# Could change OSH
|
387 |
# zsh agrees with OSH, but it fails most test cases
|
388 |
|
389 |
single=('')
|
390 |
argv.py ${single[@]:-none} x "${single[@]:-none}"
|
391 |
## stdout: ['none', 'x', 'none']
|
392 |
|
393 |
|
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 |
|
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 |
|
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 |
|
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 |
|
427 |
s='abc'
|
428 |
echo ${s[@]}
|
429 |
echo ${s[*]}
|
430 |
## STDOUT:
|
431 |
abc
|
432 |
abc
|
433 |
## END
|
434 |
|
435 |
|
436 |
|
437 |
$SH -c 'shopt -s strict_array; s="abc"; echo ${s[@]}'
|
438 |
echo status=$?
|
439 |
|
440 |
$SH -c 'shopt -s strict_array; s="abc"; echo ${s[*]}'
|
441 |
echo status=$?
|
442 |
|
443 |
## status: 0
|
444 |
## STDOUT:
|
445 |
status=1
|
446 |
status=1
|
447 |
## END
|
448 |
## N-I bash/mksh STDOUT:
|
449 |
abc
|
450 |
status=0
|
451 |
abc
|
452 |
status=0
|
453 |
## END
|
454 |
|
455 |
|
456 |
set -- 'a b' 'c'
|
457 |
array1=('x y' 'z')
|
458 |
array2=("$@")
|
459 |
argv.py "${array1[@]}" "${array2[@]}"
|
460 |
## stdout: ['x y', 'z', 'a b', 'c']
|
461 |
|
462 |
|
463 |
HOME=/home/bob
|
464 |
a=(~/src ~/git)
|
465 |
echo "${a[@]}"
|
466 |
## stdout: /home/bob/src /home/bob/git
|
467 |
|
468 |
|
469 |
a=(-{a,b} {c,d}-)
|
470 |
echo "${a[@]}"
|
471 |
## stdout: -a -b c- d-
|
472 |
|
473 |
|
474 |
default=('1 2' '3')
|
475 |
argv.py "${undef[@]:-${default[@]}}"
|
476 |
## stdout: ['1 2', '3']
|
477 |
|
478 |
|
479 |
a=( '12 3' )
|
480 |
b=( "${a[@]}" )
|
481 |
c="${a[@]}" # This decays it to a string
|
482 |
d=${a[*]} # This decays it to a string
|
483 |
echo ${#a[0]} ${#b[0]}
|
484 |
echo ${#a[@]} ${#b[@]}
|
485 |
|
486 |
# osh is intentionally stricter, and these fail.
|
487 |
echo ${#c[0]} ${#d[0]}
|
488 |
echo ${#c[@]} ${#d[@]}
|
489 |
|
490 |
## status: 1
|
491 |
## STDOUT:
|
492 |
4 4
|
493 |
1 1
|
494 |
## END
|
495 |
## OK bash/mksh status: 0
|
496 |
## OK bash/mksh STDOUT:
|
497 |
4 4
|
498 |
1 1
|
499 |
4 4
|
500 |
1 1
|
501 |
## END
|
502 |
|
503 |
|
504 |
declare -a myarray
|
505 |
argv.py "${myarray[@]}"
|
506 |
myarray+=('x')
|
507 |
argv.py "${myarray[@]}"
|
508 |
|
509 |
f() {
|
510 |
local -a myarray
|
511 |
argv.py "${myarray[@]}"
|
512 |
myarray+=('x')
|
513 |
argv.py "${myarray[@]}"
|
514 |
}
|
515 |
f
|
516 |
## STDOUT:
|
517 |
[]
|
518 |
['x']
|
519 |
[]
|
520 |
['x']
|
521 |
## END
|
522 |
|
523 |
|
524 |
a=()
|
525 |
(( a[99]=1 )) # osh doesn't parse index assignment outside arithmetic yet
|
526 |
echo len=${#a[@]}
|
527 |
argv.py "${a[@]}"
|
528 |
echo "unset=${a[33]}"
|
529 |
echo len-of-unset=${#a[33]}
|
530 |
## STDOUT:
|
531 |
len=1
|
532 |
['1']
|
533 |
unset=
|
534 |
len-of-unset=0
|
535 |
## END
|
536 |
|
537 |
|
538 |
(( a[99]=1 ))
|
539 |
echo len=${#a[@]}
|
540 |
argv.py "${a[@]}"
|
541 |
echo "unset=${a[33]}"
|
542 |
echo len-of-unset=${#a[33]}
|
543 |
## STDOUT:
|
544 |
len=1
|
545 |
['1']
|
546 |
unset=
|
547 |
len-of-unset=0
|
548 |
## END
|
549 |
|
550 |
|
551 |
a=()
|
552 |
(( a[99]=1 ))
|
553 |
b=()
|
554 |
(( b[33]=2 ))
|
555 |
(( b[66]=3 ))
|
556 |
a+=( "${b[@]}" )
|
557 |
argv.py "${a[@]}"
|
558 |
argv.py "${a[99]}" "${a[100]}" "${a[101]}"
|
559 |
## STDOUT:
|
560 |
['1', '2', '3']
|
561 |
['1', '2', '3']
|
562 |
## END
|
563 |
|
564 |
|
565 |
# mksh doesn't support this syntax! It's a bash extension.
|
566 |
(( a[33]=1 ))
|
567 |
(( a[66]=2 ))
|
568 |
(( a[99]=2 ))
|
569 |
argv.py "${a[@]:15:2}"
|
570 |
## stdout: ['1', '2']
|
571 |
## N-I mksh status: 1
|
572 |
## N-I mksh stdout-json: ""
|
573 |
|
574 |
|
575 |
shopt -u strict_arith
|
576 |
a[a]=42
|
577 |
a[a]=99
|
578 |
argv.py "${a[@]}" "${a[0]}" "${a[42]}" "${a[99]}"
|
579 |
|
580 |
## status: 0
|
581 |
## STDOUT:
|
582 |
['42', '99', '42', '99', '']
|
583 |
## END
|
584 |
|
585 |
|
586 |
shopt -u strict_arith
|
587 |
a=(1 2 3)
|
588 |
(( x = a[a] ))
|
589 |
echo $x
|
590 |
## status: 0
|
591 |
## STDOUT:
|
592 |
2
|
593 |
## END
|
594 |
|
595 |
|
596 |
x=1
|
597 |
y=2
|
598 |
a[$x$y]=foo
|
599 |
|
600 |
# not allowed by OSH parsing
|
601 |
#echo ${a[$x$y]}
|
602 |
|
603 |
echo ${a[12]}
|
604 |
echo ${#a[@]}
|
605 |
|
606 |
## STDOUT:
|
607 |
foo
|
608 |
1
|
609 |
## END
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
declare -a array
|
615 |
array[x=1]='one'
|
616 |
|
617 |
code='y=2'
|
618 |
#code='1+2' # doesn't work either
|
619 |
array[$code]='two'
|
620 |
|
621 |
argv.py "${array[@]}"
|
622 |
echo x=$x
|
623 |
echo y=$y
|
624 |
|
625 |
## STDOUT:
|
626 |
['one', 'two']
|
627 |
x=1
|
628 |
y=2
|
629 |
## END
|
630 |
## N-I dash stdout-json: ""
|
631 |
## N-I dash status: 2
|
632 |
|
633 |
|
634 |
declare -a array
|
635 |
array=(zero one two three)
|
636 |
|
637 |
echo ${array[1+2]}
|
638 |
|
639 |
code='1+2'
|
640 |
echo ${array[$code]}
|
641 |
|
642 |
## STDOUT:
|
643 |
three
|
644 |
three
|
645 |
## END
|
646 |
|
647 |
# it still dynamically parses
|
648 |
|
649 |
## OK zsh STDOUT:
|
650 |
two
|
651 |
two
|
652 |
## END
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
# note: modern versions of zsh implement this
|
658 |
|
659 |
array=(1 2 3 '')
|
660 |
|
661 |
test -v 'array[1]'
|
662 |
echo set=$?
|
663 |
|
664 |
test -v 'array[3]'
|
665 |
echo empty=$?
|
666 |
|
667 |
test -v 'array[4]'
|
668 |
echo unset=$?
|
669 |
|
670 |
## STDOUT:
|
671 |
set=0
|
672 |
empty=0
|
673 |
unset=1
|
674 |
## END
|
675 |
|
676 |
## N-I mksh STDOUT:
|
677 |
set=2
|
678 |
empty=2
|
679 |
unset=2
|
680 |
## END
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
# note: modern versions of zsh implement this
|
686 |
|
687 |
array=(1 2 3)
|
688 |
[[ -v array[1] ]]
|
689 |
echo status=$?
|
690 |
|
691 |
[[ -v array[4] ]]
|
692 |
echo status=$?
|
693 |
|
694 |
## STDOUT:
|
695 |
status=0
|
696 |
status=1
|
697 |
## END
|
698 |
|
699 |
## N-I mksh status: 1
|
700 |
## N-I mksh STDOUT:
|
701 |
## END
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
array=(1 2 3 '')
|
707 |
|
708 |
test -v 'array[1+1]'
|
709 |
echo status=$?
|
710 |
|
711 |
test -v 'array[4+1]'
|
712 |
echo status=$?
|
713 |
|
714 |
echo
|
715 |
echo dbracket
|
716 |
|
717 |
[[ -v array[1+1] ]]
|
718 |
echo status=$?
|
719 |
|
720 |
[[ -v array[4+1] ]]
|
721 |
echo status=$?
|
722 |
|
723 |
## STDOUT:
|
724 |
status=0
|
725 |
status=1
|
726 |
|
727 |
dbracket
|
728 |
status=0
|
729 |
status=1
|
730 |
## END
|
731 |
|
732 |
## N-I mksh status: 1
|
733 |
## N-I mksh STDOUT:
|
734 |
status=2
|
735 |
status=2
|
736 |
|
737 |
dbracket
|
738 |
## END
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
typeset -a array
|
744 |
array=('' nonempty)
|
745 |
|
746 |
# This feels inconsistent with the rest of bash?
|
747 |
zero=0
|
748 |
|
749 |
[[ -v array[zero+0] ]]
|
750 |
echo zero=$?
|
751 |
|
752 |
[[ -v array[zero+1] ]]
|
753 |
echo one=$?
|
754 |
|
755 |
[[ -v array[zero+2] ]]
|
756 |
echo two=$?
|
757 |
|
758 |
echo ---
|
759 |
|
760 |
i='0+0'
|
761 |
[[ -v array[i] ]]
|
762 |
echo zero=$?
|
763 |
|
764 |
i='0+1'
|
765 |
[[ -v array[i] ]]
|
766 |
echo one=$?
|
767 |
|
768 |
i='0+2'
|
769 |
[[ -v array[i] ]]
|
770 |
echo two=$?
|
771 |
|
772 |
echo ---
|
773 |
|
774 |
i='0+0'
|
775 |
[[ -v array[$i] ]]
|
776 |
echo zero=$?
|
777 |
|
778 |
i='0+1'
|
779 |
[[ -v array[$i] ]]
|
780 |
echo one=$?
|
781 |
|
782 |
i='0+2'
|
783 |
[[ -v array[$i] ]]
|
784 |
echo two=$?
|
785 |
|
786 |
|
787 |
## STDOUT:
|
788 |
zero=0
|
789 |
one=0
|
790 |
two=1
|
791 |
---
|
792 |
zero=0
|
793 |
one=0
|
794 |
two=1
|
795 |
---
|
796 |
zero=0
|
797 |
one=0
|
798 |
two=1
|
799 |
## END
|
800 |
|
801 |
## N-I mksh status: 1
|
802 |
## N-I mksh STDOUT:
|
803 |
## END
|
804 |
|
805 |
|
806 |
|
807 |
a=()
|
808 |
a[-1]=1
|
809 |
|
810 |
## status: 1
|
811 |
## STDOUT:
|
812 |
## END
|
813 |
## STDERR:
|
814 |
a[-1]=1
|
815 |
^~
|
816 |
[ stdin ]:2: fatal: Index -1 is out of bounds for array of length 0
|
817 |
## END
|
818 |
|
819 |
## OK bash STDERR:
|
820 |
bash: line 2: a[-1]: bad array subscript
|
821 |
## END
|
822 |
|
823 |
# Note: mksh interprets -1 as 0xFFFFFFFF
|
824 |
## N-I mksh status: 0
|
825 |
## N-I mksh STDERR:
|
826 |
## END
|
827 |
|
828 |
|
829 |
|
830 |
a[0]=x
|
831 |
a[5]=y
|
832 |
a[10]=z
|
833 |
[[ -v a[-1] ]] && echo 'a has -1'
|
834 |
[[ -v a[-2] ]] && echo 'a has -2'
|
835 |
[[ -v a[-5] ]] && echo 'a has -5'
|
836 |
[[ -v a[-6] ]] && echo 'a has -6'
|
837 |
[[ -v a[-10] ]] && echo 'a has -10'
|
838 |
[[ -v a[-11] ]] && echo 'a has -11'
|
839 |
|
840 |
## STDOUT:
|
841 |
a has -1
|
842 |
a has -6
|
843 |
a has -11
|
844 |
## END
|
845 |
|
846 |
## N-I mksh status: 1
|
847 |
## N-I mksh STDOUT:
|
848 |
## END
|
849 |
|
850 |
|
851 |
|
852 |
e=()
|
853 |
[[ -v e[-1] ]] && echo 'e has -1'
|
854 |
|
855 |
## status: 1
|
856 |
## STDERR:
|
857 |
[[ -v e[-1] ]] && echo 'e has -1'
|
858 |
^
|
859 |
[ stdin ]:2: fatal: -v got index -1, which is out of bounds for array of length 0
|
860 |
## END
|
861 |
|
862 |
## OK bash STDERR:
|
863 |
bash: line 2: e: bad array subscript
|
864 |
## END
|
865 |
|
866 |
## N-I mksh STDERR:
|
867 |
mksh: <stdin>[2]: syntax error: 'e[-1]' unexpected operator/operand
|
868 |
## END
|
869 |
|
870 |
|
871 |
|
872 |
case $SH in mksh|bash) exit ;; esac
|
873 |
|
874 |
a=(1 2 3)
|
875 |
var b = a
|
876 |
a+=(4 5)
|
877 |
echo "a=(${a[*]})"
|
878 |
echo "b=(${b[*]})"
|
879 |
|
880 |
## STDOUT:
|
881 |
a=(1 2 3 4 5)
|
882 |
b=(1 2 3 4 5)
|
883 |
## END
|
884 |
|
885 |
## N-I mksh/bash STDOUT:
|
886 |
## END
|
887 |
|
888 |
|
889 |
|
890 |
case $SH in mksh) exit ;; esac
|
891 |
|
892 |
a=(1)
|
893 |
unset -v 'a[-2]'
|
894 |
|
895 |
## status: 1
|
896 |
## STDOUT:
|
897 |
## END
|
898 |
## STDERR:
|
899 |
unset -v 'a[-2]'
|
900 |
^
|
901 |
[ stdin ]:4: a[-2]: Index is out of bounds for array of length 1
|
902 |
## END
|
903 |
|
904 |
## OK bash STDERR:
|
905 |
bash: line 4: unset: [-2]: bad array subscript
|
906 |
## END
|
907 |
|
908 |
## N-I mksh status: 0
|
909 |
## N-I mksh STDERR:
|
910 |
## END
|
911 |
|
912 |
|
913 |
|
914 |
case $SH in mksh) exit ;; esac
|
915 |
|
916 |
a=(1 2 3 4)
|
917 |
echo "a=(${a[*]})"
|
918 |
echo "begin=-1 -> (${a[*]: -1})"
|
919 |
echo "begin=-2 -> (${a[*]: -2})"
|
920 |
echo "begin=-3 -> (${a[*]: -3})"
|
921 |
echo "begin=-4 -> (${a[*]: -4})"
|
922 |
echo "begin=-5 -> (${a[*]: -5})"
|
923 |
|
924 |
## STDOUT:
|
925 |
a=(1 2 3 4)
|
926 |
begin=-1 -> (4)
|
927 |
begin=-2 -> (3 4)
|
928 |
begin=-3 -> (2 3 4)
|
929 |
begin=-4 -> (1 2 3 4)
|
930 |
begin=-5 -> ()
|
931 |
## END
|
932 |
|
933 |
## N-I mksh STDOUT:
|
934 |
## END
|
935 |
|
936 |
|
937 |
|
938 |
case $SH in mksh) exit ;; esac
|
939 |
|
940 |
a=(x)
|
941 |
a[9]=y
|
942 |
echo "len ${#a[@]};"
|
943 |
|
944 |
unset -v 'a[-1]'
|
945 |
echo "len ${#a[@]};"
|
946 |
echo "last ${a[@]: -1};"
|
947 |
|
948 |
## STDOUT:
|
949 |
len 2;
|
950 |
len 1;
|
951 |
last x;
|
952 |
## END
|
953 |
|
954 |
## N-I mksh STDOUT:
|
955 |
## END
|
956 |
|
957 |
|
958 |
|
959 |
case $SH in mksh) exit ;; esac
|
960 |
|
961 |
a[0]=x
|
962 |
a[2]=y
|
963 |
echo "quoted = (${a[@]@Q})"
|
964 |
|
965 |
## STDOUT:
|
966 |
quoted = (x y)
|
967 |
## END
|
968 |
|
969 |
## OK bash STDOUT:
|
970 |
quoted = ('x' 'y')
|
971 |
## END
|
972 |
|
973 |
## N-I mksh STDOUT:
|
974 |
## END
|
975 |
|
976 |
|
977 |
|
978 |
case $SH in mksh) exit ;; esac
|
979 |
|
980 |
a=(x)
|
981 |
echo "[${a[-2]}]"
|
982 |
echo $?
|
983 |
echo "[$((a[-2]))]"
|
984 |
echo $?
|
985 |
|
986 |
## STDOUT:
|
987 |
[]
|
988 |
0
|
989 |
[0]
|
990 |
0
|
991 |
## END
|
992 |
## STDERR:
|
993 |
echo "[${a[-2]}]"
|
994 |
^
|
995 |
[ stdin ]:4: Index -2 out of bounds for array of length 1
|
996 |
echo "[$((a[-2]))]"
|
997 |
^
|
998 |
[ stdin ]:6: Index -2 out of bounds for array of length 1
|
999 |
## END
|
1000 |
|
1001 |
## OK bash STDERR:
|
1002 |
bash: line 4: a: bad array subscript
|
1003 |
bash: line 6: a: bad array subscript
|
1004 |
## END
|
1005 |
|
1006 |
## N-I mksh status: 0
|
1007 |
## N-I mksh STDOUT:
|
1008 |
## END
|
1009 |
## N-I mksh STDERR:
|
1010 |
## END
|