1 #
2 # Constructs borrowed from ksh. Hm I didn't realize zsh also implements these!
3 # mksh implements most too.
4
5 #### C-style for loop
6 n=10
7 for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
8 do
9 if test $a = 3; then
10 continue
11 fi
12 if test $a = 6; then
13 break
14 fi
15 echo $a
16 done
17 ## status: 0
18 ## STDOUT:
19 1
20 2
21 4
22 5
23 ## END
24
25 #### For loop with and without semicolon
26 for ((a=1; a <= 3; a++)); do
27 echo $a
28 done
29 for ((a=1; a <= 3; a++)) do
30 echo $a
31 done
32 ## status: 0
33 ## STDOUT:
34 1
35 2
36 3
37 1
38 2
39 3
40 ## END
41
42 #### Accepts { } syntax too
43 for ((a=1; a <= 3; a++)) {
44 echo $a
45 }
46 ## STDOUT:
47 1
48 2
49 3
50 ## END
51
52 #### Empty init
53 i=1
54 for (( ;i < 4; i++ )); do
55 echo $i
56 done
57 ## status: 0
58 ## STDOUT:
59 1
60 2
61 3
62 ## END
63
64 #### Empty init and cond
65 i=1
66 for (( ; ; i++ )); do
67 if test $i = 4; then
68 break
69 fi
70 echo $i
71 done
72 ## status: 0
73 ## STDOUT:
74 1
75 2
76 3
77 ## END
78
79 #### Infinite loop with ((;;))
80 a=1
81 for (( ; ; )); do
82 if test $a = 4; then
83 break
84 fi
85 echo $((a++))
86 done
87 ## status: 0
88 ## STDOUT:
89 1
90 2
91 3
92 ## END
93
94
95 #### Arith lexer mode
96
97 # bash is lenient; zsh disagrees
98
99 for ((i = '3'; i < '5'; ++i)); do echo $i; done
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
104 ## STDOUT:
105 3
106 4
107 3
108 4
109 3
110 4
111 3
112 4
113 ## END
114 ## OK zsh status: 1
115 ## OK zsh STDOUT:
116 ## END
117