Sep 16, 2023 4 min read

Bash Break and Continue

Learn Bash Break and Continue with our step-by-step tutorial. In Bash, break and continue, allows you to regulate how a loop is executed.

Bash Break and Continue
Table of Contents

Introduction

Loops allow you to repeat a command or a set of commands until a given condition is met. However, you may need to change the loop's flow and end the loop or only the current iteration on occasion.

break and continue commands in Bash allow you to regulate how a loop is executed.

In this tutorial, we will discuss ⁣break and continue command. We will also address a few FAQs on Bash Break and Continue Commands.

Bash break Statement

The break statement ends the current loop and transfers program control to the command that comes after it. It's used to get out of a loop like for, while, until, or select. The break statement is written in the following format:

break [n]

The argument [n] is optional and must be greater than or equal to 1. The n-th enclosing loop is exited when [n] is specified. break 1 is the same as break.

Take a look at the examples below to better understand how to utilize the break statement.

Once the current iterated item is equal to 2 in the script below, the while loop will be interrupted:

i=0

while [[ $i -lt 5 ]]
do
  echo "Number: $i"
  ((i++))
  if [[ $i -eq 2 ]]; then
    break
  fi
done

echo 'All Done!'
Output

Number: 0
Number: 1
All Done!

Here's an example of nested for loops with the break statement.

break terminates the innermost enclosing loop when the argument [n] is not specified. The outer loops are unfinished:

for i in {1..3}; do
  for j in {1..3}; do
    if [[ $j -eq 2 ]]; then
      break
    fi
    echo "j: $j"
  done
  echo "i: $i"
done

echo 'All Done!'
Output

j: 1
i: 1
j: 1
i: 2
j: 1
i: 3
All Done!

break 2 is the way to get out of the outer circle. break is told to end the second enclosing loop by argument 2:

for i in {1..3}; do
  for j in {1..3}; do
    if [[ $j -eq 2 ]]; then
      break 2
    fi
    echo "j: $j"
  done
  echo "i: $i"
done

echo 'All Done!'
Output

j: 1
All Done!

Bash continue Statement

For the current iteration, the continue statement bypasses the remaining commands inside the body of the enclosing loop and passes program control to the loop's next iteration.

The continue statement has the following syntax:

continue [n]

Optionally, the [n] argument might be greater than or equal to 1. The n-th enclosing loop is resumed when [n] is specified. continue 1 is the same as continue.

Once the current iterated item equals 2, the continue statement causes execution to return to the beginning of the loop and continue with the next iteration in the example below.

i=0

while [[ $i -lt 5 ]]; do
  ((i++))
  if [[ "$i" == '2' ]]; then
    continue
  fi
  echo "Number: $i"
done

echo 'All Done!'
Output

Number: 1
Number: 3
Number: 4
Number: 5
All Done!

The script below prints numbers that are divisible by 9 from 1 to 50.

The continue statement skips the echo command and passes control to the next iteration of the loop if a number is not divisible by 9.

for i in {1..50}; do
  if [[ $(( $i % 9 )) -ne 0 ]]; then
    continue
  fi
  echo "Divisible by 9: $i"
done
Output

Divisible by 9: 9
Divisible by 9: 18
Divisible by 9: 27
Divisible by 9: 36
Divisible by 9: 45

FAQs on Bash Break and Continue Commands

How do I use the break command in a loop? 

You can use the break command within a loop to terminate the loop immediately. When encountered, the break statement transfers control to the first line after the loop.

Can I use the break command outside of loops? 

No, the break command can only be used within loop constructs in Bash. It will cause a syntax error if used outside a loop.

What is the purpose of the continue command in Bash? 

The continue command is used to skip the remaining statements in the loop and move on to the next iteration. It allows you to bypass the remaining code in a loop and start the next iteration immediately.

Can I use the continue command outside of loops? 

No, the continue command can only be used within loop constructs in Bash. It will cause a syntax error if used outside a loop.

Can I use the break or continue command in nested loops? 

Yes, both the break and continue commands work in nested loop structures. When used in a nested loop, these commands affect the innermost loop where they are encountered.

How can I break out of or skip an outer loop from within a nested loop? 

To break out of an outer loop from within a nested loop, you can use labels and the break command. Define a label before the outer loop and use the break command with that label to terminate the desired loop.

Is it possible to use the break or continue command in a switch statement? 

No, the break and continue commands do not work within a switch statement in Bash. Only the break command is recognized in this context, and it is used to exit the entire switch statement, not just a specific case.

Conclusion

One of the most fundamental ideas in programming languages is the loop. Loops are useful for automating repeated operations in scripting languages like Bash.

To exit the current loop, use the break statement. The continue statement is used to end the current loop iteration and start the next one.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.