1 #!/bin/bash
2
3 ### implicit for loop
4 # This is like "for i in $@".
5 func() {
6 for i; do
7 echo $i
8 done
9 }
10 func 1 2 3
11 # stdout-json: "1\n2\n3\n"
12
13 ### empty for loop (has "in")
14 set -- 1 2 3
15 for i in ; do
16 echo $i
17 done
18 # stdout-json: ""
19 # BUG mksh stdout-json: "1\n2\n3\n"
20
21 ### for loop with invalid identifier
22 # should be compile time error, but runtime error is OK too
23 for - in a b c; do
24 echo hi
25 done
26 # stdout-json: ""
27 # status: 2
28 # OK bash/mksh status: 1
29
30 ### Tilde expansion within for loop
31 HOME=/home/bob
32 for name in ~/src ~/git; do
33 echo $name
34 done
35 # stdout-json: "/home/bob/src\n/home/bob/git\n"
36
37 ### Brace Expansion within Array
38 for i in -{a,b} {c,d}-; do
39 echo $i
40 done
41 # stdout-json: "-a\n-b\nc-\nd-\n"
42 # N-I dash stdout-json: "-{a,b}\n{c,d}-\n"
43
44 ### using loop var outside loop
45 func() {
46 for i in a b c; do
47 echo $i
48 done
49 echo $i
50 }
51 func
52 # status: 0
53 # stdout-json: "a\nb\nc\nc\n"
54
55 ### continue
56 for i in a b c; do
57 echo $i
58 if test $i = b; then
59 continue
60 fi
61 echo $i
62 done
63 # status: 0
64 # stdout-json: "a\na\nb\nc\nc\n"
65
66 ### break
67 for i in a b c; do
68 echo $i
69 if test $i = b; then
70 break
71 fi
72 done
73 # status: 0
74 # stdout-json: "a\nb\n"
75
76 ### continue at top level is error
77 # NOTE: bash and mksh both print warnings, but don't exit with an error.
78 continue
79 echo bad
80 # N-I bash/dash/mksh status: 0
81 # N-I bash/dash/mksh stdout: bad
82 # stdout-json: ""
83 # status: 1
84
85 ### break at top level is error
86 break
87 echo bad
88 # N-I bash/dash/mksh status: 0
89 # N-I bash/dash/mksh stdout: bad
90 # stdout-json: ""
91 # status: 1
92
93 ### while in while condition
94 # This is a consequence of the grammar
95 while while true; do echo cond; break; done
96 do
97 echo body
98 break
99 done
100 # stdout-json: "cond\nbody\n"
101
102 ### while in pipe
103 i=0
104 find tests/ | while read $path; do
105 i=$((i+1))
106 #echo $i
107 done
108 # This Because while loop was in a subshell
109 echo $i
110 # stdout: 0
111
112 ### while in pipe with subshell
113 i=0
114 find bin/ -type f | ( while read path; do
115 i=$((i+1))
116 #echo $i
117 done
118 echo $i )
119 # stdout: 1
120
121 ### until loop
122 # This is just the opposite of while? while ! cond?
123 until false; do
124 echo hi
125 break
126 done
127 # stdout: hi