1 ## compare_shells: bash-4.4 zsh
2
3 # Constructs borrowed from ksh. Hm I didn't realize zsh also implements these!
4 # mksh implements most too.
5
6 #### C-style for loop
7 n=10
8 for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
9 do
10 if test $a = 3; then
11 continue
12 fi
13 if test $a = 6; then
14 break
15 fi
16 echo $a
17 done
18 ## status: 0
19 ## STDOUT:
20 1
21 2
22 4
23 5
24 ## END
25
26 #### For loop with and without semicolon
27 for ((a=1; a <= 3; a++)); do
28 echo $a
29 done
30 for ((a=1; a <= 3; a++)) do
31 echo $a
32 done
33 ## status: 0
34 ## STDOUT:
35 1
36 2
37 3
38 1
39 2
40 3
41 ## END
42
43 #### Accepts { } syntax too
44 for ((a=1; a <= 3; a++)) {
45 echo $a
46 }
47 ## STDOUT:
48 1
49 2
50 3
51 ## END
52
53 #### Empty init
54 i=1
55 for (( ;i < 4; i++ )); do
56 echo $i
57 done
58 ## status: 0
59 ## STDOUT:
60 1
61 2
62 3
63 ## END
64
65 #### Empty init and cond
66 i=1
67 for (( ; ; i++ )); do
68 if test $i = 4; then
69 break
70 fi
71 echo $i
72 done
73 ## status: 0
74 ## STDOUT:
75 1
76 2
77 3
78 ## END
79
80 #### Infinite loop with ((;;))
81 a=1
82 for (( ; ; )); do
83 if test $a = 4; then
84 break
85 fi
86 echo $((a++))
87 done
88 ## status: 0
89 ## STDOUT:
90 1
91 2
92 3
93 ## END
94
95
96 #### Arith lexer mode
97
98 # bash is lenient; zsh disagrees
99
100 for ((i = '3'; i < '5'; ++i)); do echo $i; done
101 for ((i = "3"; i < "5"; ++i)); do echo $i; done
102 for ((i = $'3'; i < $'5'; ++i)); do echo $i; done
103 for ((i = $"3"; i < $"5"; ++i)); do echo $i; done
104
105 ## STDOUT:
106 3
107 4
108 3
109 4
110 3
111 4
112 3
113 4
114 ## END
115 ## OK zsh status: 1
116 ## OK zsh STDOUT:
117 ## END
118
119
120 #### Integers near 31, 32, 62 bits
121
122 # Hm this was never a bug, but it's worth testing.
123 # The bug was EvalToInt() in the condition.
124
125 for base in 31 32 62; do
126
127 start=$(( (1 << $base) - 2))
128 end=$(( (1 << $base) + 2))
129
130 for ((i = start; i < end; ++i)); do
131 echo $i
132 done
133 echo ---
134 done
135
136 ## STDOUT:
137 2147483646
138 2147483647
139 2147483648
140 2147483649
141 ---
142 4294967294
143 4294967295
144 4294967296
145 4294967297
146 ---
147 4611686018427387902
148 4611686018427387903
149 4611686018427387904
150 4611686018427387905
151 ---
152 ## END
153
154
155 #### Condition that's greater than 32 bits
156
157 iters=0
158
159 for ((i = 1 << 32; i; ++i)); do
160 echo $i
161 iters=$(( iters + 1 ))
162 if test $iters -eq 5; then
163 break
164 fi
165 done
166
167 ## STDOUT:
168 4294967296
169 4294967297
170 4294967298
171 4294967299
172 4294967300
173 ## END