Table of contents
1.
Introduction
2.
The Break Statement
2.1.
Syntax
2.2.
Example
2.3.
Help Command
3.
The Continue Statement
3.1.
Syntax
3.2.
Example
3.3.
Help Command
4.
Frequently Asked Questions
4.1.
What do you mean by a shell?
4.2.
What is Shell Variable?
4.3.
What does a shell scripting superblock mean?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Shell Scripting - Break and Continue

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Loops are very crucial in every programming language. Loops are used to perform a particular task many times until the required condition is met. It's primarily used to automate routine tasks. 

Sometimes we need to control a loop by skipping the iteration of a loop or by stopping a loop. This article will cover the two statements, Break and Continue, that we use to control the shell scripting loops.

The Break Statement

The break statement will cause the loop to end after all of the code up to the break statement has been executed. It is used to terminate the whole loop. It can be used within the three main loops for, while, and until. 

The break Statement

Syntax

The syntax for the break statement is as follows:

break

 

The syntax for the break statement for nested loop is as follows:

break N

 

In this case, the Nth enclosed loop is specified by the value of N. The value of N is one by default.

 

Example

Look at the example below. This simple while loop iterates through a range of integers from 6 to 15. When the condition evaluates to true ($val = 10), the break statement is executed, ending the loop and skipping the remaining iterations.

#!/bin/sh
val=6
while [ $val -lt 15 ]
do
   echo $val
   if [ $val -eq 10 ]
   then
      break
   fi
   val=`expr $val + 1`
done

 

Output:

6
7
8
9
10

 

When val=10, the break will exit out of the loop.

 

Help Command

To find out more about the break statement, use the help command.

Syntax: 

$ help break

 

Output:

Output of $help break

The Continue Statement

What if you want to skip the code block when a specific condition is met and not completely end the loop? To solve this, you can use a continue statement. Except for the fact that it just ends the current loop iteration and not the whole loop, the continue statement is similar to the break command.

The Continue Statement

Syntax

The syntax for the continue statement is as follows:

continue

 

The syntax for the continue statement for nested loop is as follows:

continue  N

 

In this case, the Nth enclosed loop to continue from is specified by the value of N. The value of N is one by default.

 

Example

Look at the example below. This simple for loop iterates through a range of integers from 6 to 12. When the condition evaluates to true ($var = 10), the continue statement is executed, skipping the value 10 and printing the remaining iterations.

#!bin/sh
for var in {6..12..1}
do
  if [[ $var -eq 10 ]]
  then
      continue
  fi
  echo "value ${var}"
done

 

Output:

value 6
value 7
value 8
value 9
value 11
value 12

 

Help Command

To find out more about the continue statement, use the help command.

Syntax

$ help continue

 

Output:

Output of $help continue

Frequently Asked Questions

What do you mean by a shell?

Shell is a command-line interpreter that converts user-provided commands into syntax that the kernel can understand. The required program is called when the shell evaluates the command entered at the terminal.

What is Shell Variable?

A shell script or program's vital part is a shell variable. The variable enables the shell to control the data that is saved inside a shell program. Usually, it is kept in a string variable.

What does a shell scripting superblock mean?

A program holding particular file systems' entries is known as a superblock. Among its known properties are the block's size, counts of filled and empty blocks, use data, block group sizes, and the position and size of file tables.

Conclusion

We discussed the break statement and the continue statement in Shell Scripting. We discussed the syntax, example, and block diagrams of break and continue statements in shell scripting.

We hope this blog has helped you enhance your knowledge of the break and continue Statement in Shell Scripting. If you want to learn more, then check out our articles.

âš¡ Shell Scripting - Read User Input.

âš¡ Shell Scripting - Array variables.

âš¡ Shell Scripting  - While Loop.

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

 

We wish you Good Luck! Keep coding and keep reading Ninja!!

Live masterclass