• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash break and continue

Written by Admin, Updated On October 30, 2020
bash, loop, terminal

Using loops you can run multiple commands until the condition is satisfied. But sometimes you need to divert the flow of loop or terminate the loop iteration. In such cases, you can use break and continue statements in bash to handle the loop execution. In this guide we will how to use break and continue statements in Bash script.

Bash break and continue

Bash break Statement#

The break statement will terminate the current loop and pass the control to the following statement or command. Generally, it is used to exit from a for, while, until or select loops.

Following is the basic syntax for the break statement:

break [n]

Here, the [n] is an optional argument and must greater than or equal to 1. [n] is used to terminate and exit from the n-th enclosing loop. break 1 is similar to break.

Let’s see an example for better understanding. In following script, in while loop when the third iteration occur it should terminate:

i=0

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

echo 'That's it!'
Number: 0
Number: 1
Number: 2
That's it!

Another example of using the break statement inside nested for loops. If the argument [n] is not provided then break terminates the current inner loop and outer loops will not terminate.

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 'That's it!'
j: 1
i: 1
j: 1
i: 2
j: 1
i: 3
That's it!

To exit from the outer loop, you should use the break 2. Which will break and terminate the second enclosing loop:

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 'That's it!'
j: 1
That's it!

Bash continue Statement#

The continue statement will skip the remaining commands inside the body of the enclosing loop for the current iteration. Then it will pass the program control to the next iteration of the loop.

The common syntax of the continue statement is as follows:

continue [n]

Same as break, the [n] is an optional argument and can be greater or equal to 1. When [n] is given, the n-th enclosing loop is resumed. continue 1 is similar to continue.

In the example below, once the current iterated item is equal to 2, the continue statement will cause execution to return to the beginning of the loop and to continue with the next iteration.

i=0

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

echo 'That's it!'
Number: 1
Number: 3
Number: 4
Number: 5
That's it!

In below example, it will show the numbers from 1 through 50 which are divisible by 7. If a number is not divisible by 7, the continue statement skips the echo command and pass control to the next iteration of the loop.

for i in {1..50}; do
  if [[ $(( $i % 7 )) -ne 0 ]]; then
    continue
  fi
  echo "Divisible by 7: $i"
done
Divisible by 7: 7
Divisible by 7: 14
Divisible by 7: 21
Divisible by 7: 28
Divisible by 7: 35
Divisible by 7: 42
Divisible by 7: 49

Conclusion#

In each programming languages loops are basic concepts and useful for automating repetitive tasks. In Bash, the break and continue statement are used to terminate current loop iteration and divert control to specific statement accordingly.

If you have any question or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash Sequence Expression (Range)
Next Article   How to Install LibreOffice 7.0 on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook