Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Small fixes to bash loops chapter #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
bobbyiliev merged 1 commit into bobbyiliev:main from marcelozarate:bash-loops-fixes
Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions ebook/en/content/011-bash-loops.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ done
A quick rundown of the example:

* First, we specify a list of users and store the value in a variable called `$users`.
* After that, we start our `for` loop with the `for` keyword
* After that, we start our `for` loop with the `for` keyword.
* 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.
* Then we specify the `in` keyword followed by our list that we will loop through
* On the next line, we use the `do` keyword, which indicates what we will do for each iteration of the loop
* Then we specify the commands that we want to run
* Finally, we close the loop with the `done` keyword
* Then we specify the `in` keyword followed by our list that we will loop through.
* On the next line, we use the `do` keyword, which indicates what we will do for each iteration of the loop.
* Then we specify the commands that we want to run.
* Finally, we close the loop with the `done` keyword.

You can also use `for` to process a series of numbers. For example here is one way to loop through from 1 to 10:

Expand All @@ -54,7 +54,7 @@ The structure of a while loop is quite similar to the `for` loop:
```bash
while [ your_condition ]
do
your_conditions
your_commands
done
```

Expand Down Expand Up @@ -137,16 +137,15 @@ for i in 1 2 3 4 5
do
if [ $i –eq 2 ]
then
echo "skipping number 2"
echo "skipping number 2"
continue
fi
echo "I is equal to $i"
echo "i is equal to $i"
done
```

We can also use continue command in similar way to break command for controlling multiple loops.


* `break` tells your bash script to end the loop straight away.

The syntax of the break statement takes the following form:
Expand All @@ -170,7 +169,7 @@ do
fi
((num++))
done
echo "Loop completed"
echo "Loop completed"
```

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.
Expand All @@ -182,14 +181,14 @@ Example:

for (( a = 1; a < 10; a++ ))
do
echo "outer loop: $a"
echo "outer loop: $a"
for (( b = 1; b < 100; b++ ))
do
if [ $b –gt 5 ]
then
break 2
fi
echo "Inner loop: $b "
echo "Inner loop: $b "
done
done
```
Expand Down

AltStyle によって変換されたページ (->オリジナル) /