[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJuly 21, 2023

How to use case Bash break and continue.

How To Use Case Bash Break And Continue.

Nexonhost Dedicated Servers , Virtual Servers

 

Bash break Statement

In Bash, break and continue statements allows you to control the loop execution.

The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is used to exit from a for, while, until , or select loop. s The syntax of the break statement takes the following form:

  • break [n]

[n] is an optional argument and must be greater than or equal to 1. When [n] is provided, the n-th enclosing loop is exited. break 1 is equivalent to break.

To better understand how to use the break statement, let’s take a look at the following examples.

In the script below, the execution of the while loop will be interrupted once the current iterated item is equal to 2:

  • i=0
  • while [[ $i -lt 5 ]]
  • do
  • echo “Number: $i”
  • ((i++))
  • if [[ $i -eq 2 ]]; then
  • break
  • fi
  • done
  • echo ‘All Done!’
  • Number: 0
  • Number: 1
  • All Done!

Here is an example of using the break statement inside nested for loops .

When the argument [n] is not given, break terminates the innermost enclosing loop. The outer loops are not terminated:

  • 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 ‘All Done!’
  • j: 1
  • i: 1
  • j: 1
  • i: 2
  • j: 1
  • i: 3
  • All Done!

If you want to exit from the outer loop, use break 2. Argument 2 tells break to 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 ‘All Done!’
  • j: 1
  • All Done!

 

Bash continue Statement

The continue statement skips the remaining commands inside the body of the enclosing loop for the current iteration and passes program control to the next iteration of the loop.

The syntax of the continue statement is as follows:

  • continue [n]

The [n] argument is optional and can be greater than or equal to 1. When [n] is given, the n-th enclosing loop is resumed. continue 1 is equivalent 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 ‘All Done!’
  • Number: 1
  • Number: 3
  • Number: 4
  • Number: 5
  • All Done!

The following script prints numbers from 1 through 50 that are divisible by 9.

If a number is not divisible by 9, 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 % 9 )) -ne 0 ]]; then
  • continue
  • fi
  • echo “Divisible by 9: $i”
  • done
  • Divisible by 9: 9
  • Divisible by 9: 18
  • Divisible by 9: 27
  • Divisible by 9: 36
  • Divisible by 9: 45

 

Conclusion

Loops are one of the fundamental concepts of programming languages. In scripting languages such as Bash, loops are useful for automating repetitive tasks.

The break statement is used to exit the current loop. The continue statement is used to exit the current iteration of a loop and begin the next iteration.

Keep reading
How to Create a Password Protected ZIP File.
September 12, 2023

How to Create a Password Protected ZIP File.

How To Create A Password Protected ZIP File. Linux provides Zip command to work with a file like compressing the file and decompressing with a password. It’s not come with built-in you need to install from an external source. The Zip command has two different utility(zip and unzip). zip is used…

Read article
How to create multiple users using shell script.
September 12, 2023

How to create multiple users using shell script.

How To Create Multiple Users Using Shell Script. In Linux, we create users for multiple purposes, so in Linux, it’s very common to make new users depending on the tasks. So sometimes we need to create more than one user or multiple users. We can’t do it one by one as it would be very…

Read article
How to use Paste Command (Merge Lines)
September 12, 2023

How to use Paste Command (Merge Lines)

How To Use Paste Command (Merge Lines) In this tutorial, we will explain how to use the paste command. paste is a command that allows you to merge lines of files horizontally. It outputs lines consisting of the sequentially corresponding lines of each file specified as an argument, separated by…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00