1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# Test arithmetic expressions in all their different contexts. |
4 |
|
5 |
# $(( 1 + 2 )) |
6 |
# (( a=1+2 )) |
7 |
# ${a[ 1 + 2 ]} |
8 |
# ${a : 1+2 : 1+2} |
9 |
# a[1 + 2]=foo |
10 |
|
11 |
#### Multiple right brackets inside expression |
12 |
a=(1 2 3) |
13 |
echo ${a[a[0]]} ${a[a[a[0]]]} |
14 |
## stdout: 2 3 |
15 |
## N-I zsh status: 0 |
16 |
## N-I zsh stdout-json: "\n" |
17 |
|
18 |
#### Slicing of string with constants |
19 |
s='abcd' |
20 |
echo ${s:0} ${s:0:4} ${s:1:1} |
21 |
## stdout: abcd abcd b |
22 |
|
23 |
#### Slicing of string with variables |
24 |
s='abcd' |
25 |
zero=0 |
26 |
one=1 |
27 |
echo ${s:$zero} ${s:$zero:4} ${s:$one:$one} |
28 |
## stdout: abcd abcd b |
29 |
|
30 |
#### Array index on LHS of assignment |
31 |
a=(1 2 3) |
32 |
zero=0 |
33 |
a[zero+5-4]=X |
34 |
echo ${a[@]} |
35 |
## stdout: 1 X 3 |
36 |
## OK zsh stdout: X 2 3 |
37 |
|
38 |
#### Array index on LHS with indices |
39 |
a=(1 2 3) |
40 |
a[a[1]]=X |
41 |
echo ${a[@]} |
42 |
## stdout: 1 2 X |
43 |
## OK zsh stdout: X 2 3 |
44 |
|
45 |
#### Slicing of string with expressions |
46 |
# mksh accepts ${s:0} and ${s:$zero} but not ${s:zero} |
47 |
# zsh says unrecognized modifier 'z' |
48 |
s='abcd' |
49 |
zero=0 |
50 |
echo ${s:zero} ${s:zero+0} ${s:zero+1:zero+1} |
51 |
## stdout: abcd abcd b |
52 |
## BUG mksh stdout-json: "" |
53 |
## BUG mksh status: 1 |
54 |
## BUG zsh stdout-json: "" |
55 |
## BUG zsh status: 1 |
56 |
|
57 |
#### Ambiguous colon in slice |
58 |
s='abcd' |
59 |
echo $(( 0 < 1 ? 2 : 0 )) # evalutes to 2 |
60 |
echo ${s: 0 < 1 ? 2 : 0 : 1} # 2:1 -- TRICKY THREE COLONS |
61 |
## stdout-json: "2\nc\n" |
62 |
## BUG mksh stdout-json: "2\n" |
63 |
## BUG mksh status: 1 |
64 |
## BUG zsh stdout-json: "2\n" |
65 |
## BUG zsh status: 1 |
66 |
|
67 |
#### Triple parens should be disambiguated |
68 |
# The first paren is part of the math, parens 2 and 3 are a single token ending |
69 |
# arith sub. |
70 |
((a=1 + (2*3))) |
71 |
echo $a $((1 + (2*3))) |
72 |
## stdout: 7 7 |
73 |
|
74 |
#### Quadruple parens should be disambiguated |
75 |
((a=1 + (2 * (3+4)))) |
76 |
echo $a $((1 + (2 * (3+4)))) |
77 |
## stdout: 15 15 |
78 |
|
79 |
#### Alternative $[] syntax |
80 |
echo $[1 + 2] "$[3 * 4]" |
81 |
## stdout: 3 12 |
82 |
## N-I mksh stdout: $[1 + 2] $[3 * 4] |