How to Use Break and Continue Statements in Shell Scripts

[‘

n

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.

n

$ type -a break continuern

n

Bash Builtin Commands
Bash Builtin Commands

n

Exit the loop with a Break Statement

n

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.

n

$ help breakrn

n

Break Help Command
Break Help Command

n

The basic syntax of break.

n

$ break [n]rnrnn is optionalrn

n

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.

n

#!/usr/bin/bashrnrnfor val in {1..20..2}rndorn  If [[ $val -eq 9 ]]rn  thenrn     breakrn  elsern  echo "printing ${val}"rnfirndonern

n

Break Statement
Break Statement

n

Skip an Iteration with continue Statement

n

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.

n

To access help.

n

$ help continuern

n

Continue Help
Continue Help

n

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.

n

#!/usr/bin/bashrnrnfor val in {1..20..2}rndorn  If [[ $val -eq 9 ]]rn  thenrn      continuern  firn  echo "printing ${val}"rndonern

n

Continue Statement
Continue Statement

n

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.

n

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.

n

$ type -a : truern

n

True and Colon Shell Builtin
True and Colon Shell Builtin

n

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.

n

#!/usr/bin/bashrnrnfor val in {1..20..2}rndorn  If [[ $val -eq 9 ]]rn  thenrn      truern  firn  echo "printing ${val}"rndonern

n

Pass Statement
Pass Statement

n

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

n

‘]