1 | ## compare_shells: bash mksh zsh
|
2 | ## oils_failures_allowed: 0
|
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[@]: : }
|
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 | # zsh doesn't accept this
|
147 | echo slice ${a[@]:: }
|
148 | echo status=$?
|
149 |
|
150 | ## STDOUT:
|
151 | slice a b c d e f
|
152 | status=0
|
153 |
|
154 | slice
|
155 | status=0
|
156 |
|
157 | slice
|
158 | status=0
|
159 | ## END
|
160 |
|
161 | ## BUG zsh status: 1
|
162 | ## BUG zsh STDOUT:
|
163 | slice a b c d e f
|
164 | status=0
|
165 |
|
166 | slice
|
167 | status=0
|
168 |
|
169 | ## END
|
170 |
|
171 | ## BUG mksh status: 1
|
172 | ## BUG mksh STDOUT:
|
173 | ## END
|
174 |
|
175 |
|
176 | #### Empty expression a[]
|
177 |
|
178 | a=(1 2 3)
|
179 |
|
180 | a[]=42
|
181 | echo status=$?
|
182 | echo ${a[@]}
|
183 |
|
184 | echo ${a[]}
|
185 | echo status=$?
|
186 |
|
187 | ## status: 2
|
188 | ## STDOUT:
|
189 | ## END
|
190 |
|
191 | ## OK zsh status: 1
|
192 |
|
193 | # runtime failures
|
194 |
|
195 | ## OK bash status: 0
|
196 | ## OK bash STDOUT:
|
197 | status=1
|
198 | 1 2 3
|
199 | status=1
|
200 | ## END
|
201 |
|
202 | ## BUG mksh status: 0
|
203 | ## BUG mksh STDOUT:
|
204 | status=0
|
205 | 42 2 3
|
206 | 42
|
207 | status=0
|
208 | ## END
|
209 |
|
210 |
|
211 | # Others
|
212 | # [ 1+2 -eq 3 ]
|
213 | # [[ 1+2 -eq 3 ]]
|
214 | # unset a[]
|
215 | # printf -v a[]
|
216 |
|