When the ‘break’ command is executed, the loop is terminated immediately, and the script continues to execute the next line of code after the loop. When the ‘continue’ command is executed, the script skips the rest of the code in the current iteration and moves on to the next loop iteration.
This article is about the break and continue statements in Bash, which you can use, and this guide includes the following.
- How to Use a break in Bash?
- Example 1: Guess Game
- Example 2: Print a Series of Numbers
- How to Use a continue in Bash?
- Example 1: Printing the Specific Output (with numbers)
- Example 2: Skipping a Number in the Loop
Now let’s get on with it as we explain the above.
How to Use a break in Bash?
The break statement is used to exit from a loop based on a specific condition, and here’s how you can use it in your scripts.
1. for, while, or until loop
2. do
3. command-one
4. command-two
5. if [ some condition ]; then
6. break
7. fi
8. command-three
9. done
Here’s the breakdown of the above syntax.
Lines 1, 2 & 9 are part of the loop that you’d use.
Lines 3, 4 & 8 have the conditions.
Lines 5 & 7 are a part of the ‘if statement.’
Line 6 has the break statement to exit the loop.
Now you’ll understand it clearly by following these examples.
Example 1: Guess Game
In this example, we’ve created a script where the user has to guess the favorite number between 1 and 10, and the user will be given a total of 5 tries. If you’ve guessed correctly or the number of attempts exceeds 5, the loop will exit using the break statement. Here’s the script.
#!/bin/bash
numSelect=`shuf -i 0-10 -num`
echo "You have 5 tries to guess my favorite number between 0 and 10"
for ((try=1; try<=5; try++))
do
echo -num "guess $try: "
read guess
if [[ $guess == $ranNumb ]]; then
echo "You guessed my favorite number $num"
break
fi
done
The script is named “GuessGame.sh” which is executed using this command.
$ sudo bash GuessGame.sh
When the script finds the randomly generated and the user enters the same number, it will execute the break statement to exit the loop, and the message “You guessed my favorite number $num” is printed.
Example 2: Print a Series of Numbers
In this example, there’s a script that prints a series of numbers, but when it comes to the number ‘5’ the break statement will be executed, and the loop will exit, displaying a message. Here’s the script.
#!/bin/bash
count=1
while [ $count -le 10 ]
do
printf "The current value of count is $count\n"
((count++))
if [ $count -eq 5 ]
then
printf "Count has a value of $count\n"
printf "The count has reached 5, now the break statement will be executed\n"
break
fi
done
This script is named ‘PrintNum.sh’, and we’ll execute it using this command.
$ sudo bash PrintNum.sh
The above script, in simple words, is supposed to print the numbers from one to ten along with the text, but it’ll only go to five and then exit because there’s a condition that executes the break statement once matched.
How to Use a continue in Bash?
The continue statement works so that, unlike the break, it doesn’t exit the loop when a specific condition is met; instead, it returns to the top of the continue statement. The continue syntax is similar to the break statement, which is as follows:
for, while, or until loop
do
command-one
command-two
if [ some condition ]; then
continue
fi
command-three
done
command-four
After going through the below, you’ll understand it more clearly.
Example 1: Printing the Specific Output (With Numbers)
In this example, a script is created that prints the numbers from one to ten but at nine (we’ve set that condition), it’ll also print a text along with it. Here’s the script of it.
#!/bin/bash
for num in {1..10}
do
if [[ $num == '9' ]]
then
echo "Number $num"
continue
fi
echo "$num"
done
echo "All numbers are printed, and one of them is highlighted with text"
It is named ‘conEX.sh” and to execute this; the following command is used.
$ sudo bash conEX.sh
As seen in the above image, all the numbers between 1 and 10 are printed but see what is there on number 9, and if we used the break statement, then there’d be nothing printed after 10.
Example 2: Skipping a Number in the Loop
In this example, a range of numbers (1-10) is printed while skipping the numbers, let’s say ‘2 & 9’; here’s the script.
#!/bin/bash
num=0
while [ $num -lt 10 ]
do
(( num++ ))
[ $num -eq 2 -o $num -eq 9 ] && continue
echo "$num"
done
It is named ‘skipNum.sh’, and the following command is used to execute this script.
$ sudo bash skipNum.sh
The above image shows that the numbers (2 & 9) are skipped while others are printed, and you can do almost anything that a Linux can do through scripts, and all you need to do is learn.
Conclusion
Controlling the flow of your script is required as everything will be executed, which could temper the output and completely change the results. This guide elaborates on the syntax and a few examples to help you understand the break and continue statements in bash.