1 #!/usr/bin/env bash
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 #### Empty init
44 i=1
45 for (( ;i < 4; i++ )); do
46 echo $i
47 done
48 ## status: 0
49 ## STDOUT:
50 1
51 2
52 3
53 ## END
54
55 #### Empty init and cond
56 i=1
57 for (( ; ; i++ )); do
58 if test $i = 4; then
59 break
60 fi
61 echo $i
62 done
63 ## status: 0
64 ## STDOUT:
65 1
66 2
67 3
68 ## END
69
70 #### Infinite loop with ((;;))
71 a=1
72 for (( ; ; )); do
73 if test $a = 4; then
74 break
75 fi
76 echo $((a++))
77 done
78 ## status: 0
79 ## STDOUT:
80 1
81 2
82 3
83 ## END