Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Use Break and Continue Statements in Shell Scripts - DesignLinux

Mar 24 2021
designlinux 0 Comments

In this article, we will take a look at how to use a break and continue in bash scripts. In bash, we have three main loop constructs (for, while, until). Break and continue statements are bash builtin and used to alter the flow of your loops. This concept of break and continue are available in popular programming languages like Python.

$ type -a break continue
Bash Builtin Commands
Bash Builtin Commands

Exit the loop with a Break Statement

The break statement will exit out of the loop and control is passed to the next statement in the loop. You can run the help command to get some information about the break statement.

$ help break
Break Help Command
Break Help Command

The basic syntax of break.

$ break [n]

n is optional

Take a look at the below example. This is a simple for loop that iterates over a range of values from 1 to 20 in an incremental step of 2. The conditional statement will evaluate the expression and when it is true($val = 9) then it will run the break statement and the loop will be terminated skipping the remaining iterations.

#!/usr/bin/bash

for val in {1..20..2}
do
  If [[ $val -eq 9 ]]
  then
     break
  else
  echo "printing ${val}"
fi
done
Break Statement
Break Statement

Skip an Iteration with continue Statement

What if you don’t want to completely exit out of the loop but skip the block of code when a certain condition is met? This can be done with a continue statement. The continue statement will skip the execution of the code block when a certain condition is met and the control is passed back to the loop statement for the next iteration.

To access help.

$ help continue
Continue Help
Continue Help

Take a look at the below example. This is the same example we used to demonstrate the break statement. Now when Val is evaluated to nine then the continue statement will skip all the remaining blocks of code and pass the control to for loop for the next iteration.

#!/usr/bin/bash

for val in {1..20..2}
do
  If [[ $val -eq 9 ]]
  then
      continue
  fi
  echo "printing ${val}"
done
Continue Statement
Continue Statement

If you knew python then break and continue behavior is the same in python too. But python provides one more loop control statement called a pass.

Pass is like a null statement and the interpreter will read it but will not perform any operation. It simply results in no operation. Bash doesn’t provide a similar statement but we can emulate this behavior using true keyword or colon(:). Both true and colon are shell builtin and not perform any operation.

$ type -a : true
True and Colon Shell Builtin
True and Colon Shell Builtin

Take a look at the below example. When a conditional statement is evaluated to be true($val = 9) then the true statement will do nothing and the loop will continue.

#!/usr/bin/bash

for val in {1..20..2}
do
  If [[ $val -eq 9 ]]
  then
      true
  fi
  echo "printing ${val}"
done
Pass Statement
Pass Statement

That’s it for this article. We would love to hear your valuable feedback and any tips you have.

Related

Tags: Learn Shell Scripting, Shell Scripting Tips

How to Use until Loop in Your Shell Scripts

Prev Post

10 Best Udemy Google Cloud Platform Courses in 2021

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
  • 307
  • 574,797

DesignLinux.com © All rights reserved

Go to mobile version