You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ebook/en/content/011-bash-loops.md
+11-12
Original file line number
Diff line number
Diff line change
@@ -29,12 +29,12 @@ done
29
29
A quick rundown of the example:
30
30
31
31
* First, we specify a list of users and store the value in a variable called `$users`.
32
-
* After that, we start our `for` loop with the `for` keyword
32
+
* After that, we start our `for` loop with the `for` keyword.
33
33
* Then we define a new variable which would represent each item from the list that we give. In our case, we define a variable called `user`, which would represent each user from the `$users` variable.
34
-
* Then we specify the `in` keyword followed by our list that we will loop through
35
-
* On the next line, we use the `do` keyword, which indicates what we will do for each iteration of the loop
36
-
* Then we specify the commands that we want to run
37
-
* Finally, we close the loop with the `done` keyword
34
+
* Then we specify the `in` keyword followed by our list that we will loop through.
35
+
* On the next line, we use the `do` keyword, which indicates what we will do for each iteration of the loop.
36
+
* Then we specify the commands that we want to run.
37
+
* Finally, we close the loop with the `done` keyword.
38
38
39
39
You can also use `for` to process a series of numbers. For example here is one way to loop through from 1 to 10:
40
40
@@ -54,7 +54,7 @@ The structure of a while loop is quite similar to the `for` loop:
54
54
```bash
55
55
while [ your_condition ]
56
56
do
57
-
your_conditions
57
+
your_commands
58
58
done
59
59
```
60
60
@@ -137,16 +137,15 @@ for i in 1 2 3 4 5
137
137
do
138
138
if [ $i –eq 2 ]
139
139
then
140
-
echo“skipping number 2”
140
+
echo"skipping number 2"
141
141
continue
142
142
fi
143
-
echo“I is equal to $i”
143
+
echo"i is equal to $i"
144
144
done
145
145
```
146
146
147
147
We can also use continue command in similar way to break command for controlling multiple loops.
148
148
149
-
150
149
*`break` tells your bash script to end the loop straight away.
151
150
152
151
The syntax of the break statement takes the following form:
@@ -170,7 +169,7 @@ do
170
169
fi
171
170
((num++))
172
171
done
173
-
echo“Loop completed”
172
+
echo"Loop completed"
174
173
```
175
174
176
175
We can also use break command with multiple loops. If we want to exit out of current working loop whether inner or outer loop, we simply use break but if we are in inner loop & want to exit out of outer loop, we use break 2.
0 commit comments