Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash break and continue - DesignLinux

Oct 30 2020
designlinux 0 Comments

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.

Related

Tags: bash, loop, terminal

Rmmod Command in Linux

Prev Post

How to Monitor Performance Of CentOS 8/7 Server Using Netdata

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 485
  • 1,055,257

DesignLinux.com © All rights reserved

Go to mobile version