1 ## compare_shells: bash mksh zsh
2 ## oils_failures_allowed: 1
3
4 # Test arithmetic expressions in all their different contexts.
5
6 # $(( 1 + 2 ))
7 # (( a=1+2 ))
8 # ${a[ 1 + 2 ]}
9 # ${a : 1+2 : 1+2}
10 # a[1 + 2]=foo
11
12 #### Multiple right brackets inside expression
13
14 a=(1 2 3)
15 echo ${a[a[0]]} ${a[a[a[0]]]}
16
17 ## STDOUT:
18 2 3
19 ## END
20
21 ## N-I zsh status: 0
22 ## N-I zsh STDOUT:
23
24 ## END
25
26 #### Slicing of string with constants
27 s='abcd'
28 echo ${s:0} ${s:0:4} ${s:1:1}
29 ## stdout: abcd abcd b
30
31 #### Slicing of string with variables
32 s='abcd'
33 zero=0
34 one=1
35 echo ${s:$zero} ${s:$zero:4} ${s:$one:$one}
36 ## stdout: abcd abcd b
37
38 #### Array index on LHS of assignment
39 a=(1 2 3)
40 zero=0
41 a[zero+5-4]=X
42 echo ${a[@]}
43 ## stdout: 1 X 3
44 ## OK zsh stdout: X 2 3
45
46 #### Array index on LHS with indices
47 a=(1 2 3)
48 a[a[1]]=X
49 echo ${a[@]}
50 ## stdout: 1 2 X
51 ## OK zsh stdout: X 2 3
52
53 #### Slicing of string with expressions
54 # mksh accepts ${s:0} and ${s:$zero} but not ${s:zero}
55 # zsh says unrecognized modifier 'z'
56 s='abcd'
57 zero=0
58 echo ${s:zero} ${s:zero+0} ${s:zero+1:zero+1}
59 ## stdout: abcd abcd b
60 ## BUG mksh stdout-json: ""
61 ## BUG mksh status: 1
62 ## BUG zsh stdout-json: ""
63 ## BUG zsh status: 1
64
65 #### Ambiguous colon in slice
66 s='abcd'
67 echo $(( 0 < 1 ? 2 : 0 )) # evaluates to 2
68 echo ${s: 0 < 1 ? 2 : 0 : 1} # 2:1 -- TRICKY THREE COLONS
69 ## STDOUT:
70 2
71 c
72 ## END
73 ## BUG mksh/zsh STDOUT:
74 2
75 ## END
76 ## BUG mksh/zsh status: 1
77
78 #### Triple parens should be disambiguated
79 # The first paren is part of the math, parens 2 and 3 are a single token ending
80 # arith sub.
81 ((a=1 + (2*3)))
82 echo $a $((1 + (2*3)))
83 ## STDOUT:
84 7 7
85 ## END
86
87 #### Quadruple parens should be disambiguated
88 ((a=1 + (2 * (3+4))))
89 echo $a $((1 + (2 * (3+4))))
90 ## STDOUT:
91 15 15
92 ## END
93
94 #### ExprSub $[] happens to behave the same on simple cases
95 echo $[1 + 2] "$[3 * 4]"
96 ## STDOUT:
97 3 12
98 ## END
99 ## N-I mksh STDOUT:
100 $[1 + 2] $[3 * 4]
101 ## END
102
103
104 #### Empty expression (( )) $(( ))
105
106 (( ))
107 echo status=$?
108
109 echo $(( ))
110
111 #echo $[]
112
113 ## STDOUT:
114 status=1
115 0
116 ## END
117
118 #### Empty expression for (( ))
119
120 for (( ; ; )); do
121 echo one
122 break
123 done
124
125 ## STDOUT:
126 one
127 ## END
128
129 ## N-I mksh status: 1
130 ## N-I mksh STDOUT:
131 ## END
132
133 #### Empty expression in ${a[@]:slice}
134
135 a=(a b c d e f)
136
137 # space required here -- see spec/var-op-slice
138 echo slice ${a[@]: }
139 echo status=$?
140 echo
141
142 echo slice ${a[@]: : }
143 echo status=$?
144 echo
145
146 # bash and zsh differ with respect to spaces
147 #echo slice ${a[@]:: }
148
149 ## STDOUT:
150 slice a b c d e f
151 status=0
152
153 slice
154 status=0
155
156 ## END
157
158 ## BUG mksh status: 1
159 ## BUG mksh STDOUT:
160 ## END
161
162
163 #### Empty expression a[]
164
165 a=(1 2 3)
166
167 a[]=42
168 echo status=$?
169 echo ${a[@]}
170
171 echo ${a[]}
172 echo status=$?
173
174 ## status: 2
175 ## STDOUT:
176 ## END
177
178 ## OK zsh status: 1
179
180 # runtime failures
181
182 ## OK bash status: 0
183 ## OK bash STDOUT:
184 status=1
185 1 2 3
186 status=1
187 ## END
188
189 ## BUG mksh status: 0
190 ## BUG mksh STDOUT:
191 status=0
192 42 2 3
193 42
194 status=0
195 ## END
196
197
198 # Others
199 # [ 1+2 -eq 3 ]
200 # [[ 1+2 -eq 3 ]]
201 # unset a[]
202 # printf -v a[]
203