Introduction
The continue
statement is a Bash builtin that alters the flow of script loops. The concept is not unique to Bash and appears in other programming languages.
The best way to understand how the Bash continue
statement works is through hands-on examples.
This tutorial shows how to use the Bash continue
statement in Bash scripts.
Prerequisites
- A machine running Linux or macOS.
- A text editor for the examples.
- Basic knowledge of Bash scripting.
The Bash continue Statement
The Bash continue
statement resumes the following iteration in a loop or looping statement.
The syntax is:
continue <integer>
The continue
statement only has meaning when applied to loops. The integer value indicates the depth for the continue
statement. By default, the integer is 1
and writing the number is not mandatory. Increase the number to resume an outer loop statement.
Use a continue
statement as a loop control statement. For example, the continue
statement helps end the current iteration inside a loop when meeting a specific condition. Depending on the loop type, the program resumes at the next iteration or restarts the loop.
Bash continue Examples
Below are examples and explanations of working with the continue statement in Bash scripts.
Note: Never used Bash to write scripts before? Follow our guide How to Write a Bash Script.
The following primary loop constructs showcase how the statement works:
- The for loop continues at the next iteration when combined with continue.
- The continue statement restarts the
while
anduntil
loops.
The select
command also appears in the examples, even though it is not a primary loop construct. The statement functions as a loop, and the continue
statement works for any looping command.
Using Bash Continue with a for Loop
Use the continue
statement inside a conditional if to control the flow of a for
:
#!/bin/bash
for i in {1..10}
do
if [[ $i == '9' ]]
then
echo "Number $i!"
continue
fi
echo "$i"
done
echo "Done!"
The code does the following:
- Line 3 starts the
for
loop condition, iterating the variablei
from1
to10
. - Line 5 checks the
i
value. When the variable equals9
, the program echoes a message and restarts the loop at the next iteration (line 3). - Line 10 prints the number to the console only when the conditional statement in line 5 is False.
Run the script to see the results.
The output prints all the individual numbers to the console. When the condition in line 5 evaluates to True ($i == '9'
), the console echoes a message.
Using Bash Continue with Nested Loop
The continue
statement works with nested loops as well. For example, use continue 2
to resume an outer loop:
#!/bin/bash
for i in {1..5}
do
for j in {1..5}
do
if [[ $i -eq $j ]]
then
echo "$i = $j"
continue 2
fi
echo "$i =/= $j"
done
done
echo "Done!"
The program does the following:
- Line 3 starts the outer loop, incrementing the variable
i
from1
to5
. - Line 5 starts an inner for loop, incrementing the variable
j
from1
to5
for eachi
increment. - Line 7 checks if the variables
i
andj
are equal ($i -eq $j
). If they are, thecontinue 2
statement resumes the outer loop at the next iteration. However, if the values are different, the program continues as expected.
Run the script to see the program output.
Each time the two values are equal, the first number increases.
Using Bash Continue with a while Loop
Below is an example Bash script that uses the <strong>continue</strong>
statement in a while
loop:
#!/bin/bash
i=0
while [[ $i -lt 11 ]]
do
if [[ "$i" == '9' ]]
then
echo "Number $i!"
((i++))
continue
fi
echo $i
((i++))
done
Each line does the following:
- Line 3 defines a variable
i
and sets the value to0
. - Line 5 initiates a
while
loop. The end condition is wheni
is less than11
. - Line 7 states a condition check using an
if
statement. When the variablei
equals9
, the program proceeds to lines 9-11. In all other cases, the code jumps to line 13. - Lines 9-11 print a message to the console, increment
i
, and thecontinue
statement resumes the loop at line 5. - Lines 13 and 14 print the variable
i
to the console and increment it.
To see the output, run the script from the terminal.
The program prints all the numbers to the console. Due to the conditional and continue statements, a different message prints for number 9
.
Using Bash Continue with an until Loop
Combine the continue
statement with an until
loop and provide a condition to change the loop behavior for a certain value. For example, try the following script:
#!/bin/bash
i=0
until [[ $i -gt 10 ]]
do
if [[ $i -eq 9 ]]
then
echo "Number $i!"
((i++))
continue
fi
echo $i
((i++))
done
The code increments a variable and loops until the value reaches 10
. When the variable equals 9
, the program outputs a different message. The continue
statement restarts the until
loop and continues as usual.
Using Bash Continue with a select Loop
The select
command is a particular case because it is not a primary Bash scripting loop. The command creates menus that require user input. Use the continue
statement to provide a different output based on the user-selected value.
As an example, try the following Bash script:
#!/bin/bash
PS3="Choose a number: "
select i in {1..10}
do
echo "Selected number: $i"
if [[ $REPLY -eq 9 ]]
then
echo "Number $i!"
continue
fi
done
The script consists of the following elements:
- The
PS3
in line 3 is the display message for theselect
loop. - Line 5 defines the loop condition, listing numbers from
1
to10
. The variablei
stores the user input. - Line 7 echoes the selection.
- Line 8 performs a check. If the user input value is
9
, the program prints a message to the console and continues theselect
loop at the next iteration.
Run the script and test for different values to see the output.
To exit the program, press CTRL+C or add a break statement.
Conclusion
After following the examples from this tutorial, you know the purpose of a continue
statement and how to use it with loops in Bash scripting.
Next, learn how to write a Bash function.