Bash continue: How to Resume a Loop

January 25, 2022

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.

Bash continue: How To Resume A Loop

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.

Bash continue loop state diagram

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 and until 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!"
for_continue.sh bash script

The code does the following:

  • Line 3 starts the for loop condition, iterating the variable i from 1 to 10.
  • Line 5 checks the i value. When the variable equals 9, 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.

for_continue.sh terminal output

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!"
nested_continue.sh script

The program does the following:

  • Line 3 starts the outer loop, incrementing the variable i from 1 to 5.
  • Line 5 starts an inner for loop, incrementing the variable j from 1 to 5 for each i increment.
  • Line 7 checks if the variables i and j are equal ($i -eq $j). If they are, the continue 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.

nested_continue.sh terminal 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
while_continue.sh bash script code

Each line does the following:

  • Line 3 defines a variable i and sets the value to 0.
  • Line 5 initiates a while loop. The end condition is when i is less than 11.
  • Line 7 states a condition check using an if statement. When the variable i equals 9, 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 the continue 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.

while_continue.sh terminal output

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
until_cotinue.sh bash script code

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
select_continue.sh bash script code

The script consists of the following elements:

  • The PS3 in line 3 is the display message for the select loop.
  • Line 5 defines the loop condition, listing numbers from 1 to 10. The variable i 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 the select loop at the next iteration.

Run the script and test for different values to see the output.

select_continue.sh terminal 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.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
Bash declare Statement: Syntax and Examples
December 23, 2021

Although the builtin declare statement does not need to be used to explicitly declare a variable in Bash, the command is often used for...
Read more
Bash trap Command Explained
December 16, 2021

A Bash shell script can run into problems during its execution, resulting in an error signal that interrupts the script unexpectedly. This tutorial will show...
Read more
Bash case Statement Syntax and Examples
December 15, 2021

The bash case statement simplifies complex conditional statements and tests input strings...
Read more
Bash Function & How to Use It {Variables, Arguments, Return}
January 25, 2022

Learn how to use functions in Bash scripting through this hands-on tutorial with example codes.
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.