Bash Scripting – Controlling the Loop
Other Learning Articles that you may like to read
Free Courses We Offer
Paid Training Courses we Offer
You might be tempted to think that after you start a loop, you’re stuck until the loop finishes all its iterations. This is not true. A couple of commands help us control what happens inside of a loop:
- The break command
- The continue command
Each command has a different use in how to control the operation of a loop. The following sections describe how you can use these commands to control the operation of your loops.
The break command
The break command is a simple way to escape a loop in progress. You can use the break command to exit any type of loop, includingwhile and until loops.
You can use the break command in several situations. This section shows each of these methods.
Breaking out of a single loop
When the shell executes a break command, it attempts to break out of the loop that’s currently processing:
$ cat test17
#!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5 6 7 8 9 10
do
if [ $var1 -eq 5 ]
then
break
fi
echo “Iteration number: $var1”
done
echo “The for loop is completed”
$ ./test17
Iteration number : 1
Iteration number : 2
Iteration number : 3
Iteration number : 4
The for loop is completed
$
The for loop should normally have iterated through all the values specified in the list. However, when the if-then condition was satisfied, the shell executed the break command, which stopped the for loop.
This technique also works for while and until loops:
$ cat test18
#!/bin/bash
# breaking out of a while loop
var1=1
while [ $var1 -lt 10 ]
do
if [ $var1 -eq 5 ]
then
break
fi
echo “Iteration: $var1”
var1=$[ $var1 + 1 ]
done
echo “The while loop is completed”
$ ./test18
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
The while loop is completed
$
The while loop terminated when the if-then condition was met, executing the break command.
Breaking out of an inner loop
When you’re working with multiple loops, the break command automatically terminates the innermost loop you’re in:
$ cat test19
#!/bin/bash
# breaking out of an inner loop
for (( a = 1; a < 4; a++ ))
do
echo “Outer loop: $a”
for (( b = 1; b < 100; b++ ))
do
if [ $b -eq 5 ]
then
break
fi
echo “ Inner loop: $b”
done
done
$ ./test19
Outer loop: 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 2
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 3
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
$
The for statement in the inner loop specifies to iterate until the b variable is equal to 100. However, the if-then statement in the inner loop specifies that when the b variable value is equal to 5, the break command is executed. Notice that even though the inner loop is terminated with the break command, the outer loop continues working as specified.
Breaking out of an outer loop
There may be times when you’re in an inner loop but need to stop the outer loop. The break command includes a single command line parameter value:
break n
where n indicates the level of the loop to break out of. By default, n is 1, indicating to break out of the current loop. If you set n to a value of 2, the break command stops the next level of the outer loop:
$ cat test20
#!/bin/bash
# breaking out of an outer loop
for (( a = 1; a < 4; a++ ))
do
echo “Outer loop: $a”
for (( b = 1; b < 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo “ Inner loop: $b”
done
done
$ ./test20
Outer loop: 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
$
Now when the shell executes the break command, the outer loop stops.
The continue command
The continue command is a way to prematurely stop processing commands inside of a loop but not terminate the loop completely. This allows you to set conditions within a loop where the shell won’t execute commands. Here’s a simple example of using the continuecommand in a for loop:
$ cat test21
#!/bin/bash
# using the continue command
for (( var1 = 1; var1 < 15; var1++ ))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo “Iteration number: $var1”
done
$ ./test21
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
Iteration number: 10
Iteration number: 11
Iteration number: 12
Iteration number: 13
Iteration number: 14
$
When the conditions of the if-then statement are met (the value is greater than 5 and less than 10), the shell executes the continuecommand, which skips the rest of the commands in the loop, but keeps the loop going. When the if-then condition is no longer met, things return to normal.
You can use the continue command in while and until loops, but be extremely careful with what you’re doing. Remember that when the shell executes the continue command, it skips the remaining commands. If you’re incrementing your test condition variable in one of those conditions, bad things happen:
$ cat badtest3
#!/bin/bash
# improperly using the continue command in a while loop
var1=0
while echo “while iteration: $var1”
[ $var1 -lt 15 ]
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo “ Inside iteration number: $var1”
var1=$[ $var1 + 1 ]
done
$ ./badtest3 | more
while iteration: 0
Inside iteration number: 0
while iteration: 1
Inside iteration number: 1
while iteration: 2
Inside iteration number: 2
while iteration: 3
Inside iteration number: 3
while iteration: 4
Inside iteration number: 4
while iteration: 5
Inside iteration number: 5
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
while iteration: 6
$
You’ll want to make sure you redirect the output of this script to the more command so you can stop things. Everything seems to be going just fine until the if-then condition is met, and the shell executes the continue command. When the shell executes the continuecommand, it skips the remaining commands in the while loop. Unfortunately, that’s where the $var1 counter variable that is tested in thewhile test command is incremented. That means that the variable isn’t incremented, as you can see from the continually displaying output.
As with the break command, the continue command allows you to specify what level of loop to continue with a command line parameter:
continue n
where n defines the loop level to continue. Here’s an example of continuing an outer for loop:
$ cat test22
#!/bin/bash
# continuing an outer loop
for (( a = 1; a <= 5; a++ ))
do
echo “Iteration $a:”
for (( b = 1; b < 3; b++ ))
do
if [ $a -gt 2 ] && [ $a -lt 4 ]
then
continue 2
fi
var3=$[ $a * $b ]
echo “ The result of $a * $b is $var3”
done
done
$ ./test22
Iteration 1:
The result of 1 * 1 is 1
The result of 1 * 2 is 2
Iteration 2:
The result of 2 * 1 is 2
The result of 2 * 2 is 4
Iteration 3:
Iteration 4:
The result of 4 * 1 is 4
The result of 4 * 2 is 8
Iteration 5:
The result of 5 * 1 is 5
The result of 5 * 2 is 10
$
The if-then statement:
if [ $a -gt 2 ] && [ $a -lt 4 ]
then
continue 2
fi
uses the continue command to stop processing the commands inside the loop but continue the outer loop. Notice in the script output that the iteration for the value 3 doesn’t process any inner loop statements, because the continue command stopped the processing, but it continues with the outer loop processing.