[email protected]Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
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 Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Mon – Fri 9:00AM – 5:00PM