November 19, 2024
Tutorials

How to Use Break and Continue When Working With Loops in Bash

How to Use Break and Continue When Working With Loops in Bash

Loops in Bash repeat a set of commands multiple times till it finds the condition that matches. It is used to automate complex processes in scripts.

In Bash scripting, the statements Break and Continue are mainly used for controlling the execution flow, specifically in the loops. In simple words, Break terminates or stops the iteration right after it finds the first match. 

Continue works opposite to the Break statement. In the Continue statements, the loop skips the current iteration and moves forward to the next iteration.

Both statements have their specific advantages when writing scripts. You can handle specific cases more efficiently using the Break and Continue statements while working with loops in Bash.

How to Use Break and Continue When Working With Loops in Bash?

Here are two different examples for explaining the use of Break and Continues when working with loops in Bash.

How to Use Break When Working With Loops in Bash?

The below examples explains the use of a Break statement while working in Bash loops. 

Step 1: Create a Bash Script File (break.sh)

To create a Bash script file such as break.sh, you can use Nano editor:

nano break.sh

Step 2: Use Break with Loop in Bash

Within the break.sh file, write a simple bash script that iterates through a list of random numbers and breaks (stops) the loop when it finds the first number divisible by 5:

#!/bin/bash

# Break when found the numbers divisible by 5

numbers=(7 14 3 10 22 35 30 45 54 9)

echo "List of numbers: ${numbers[@]}"

for num in "${numbers[@]}"; do
if (( num % 5 == 0 )); then
    echo "Found the first numbers divisible by 5: $num"
    break
fi
Done

In this loop, the if condition will check each number in the given list. When it finds the  number which is divisible by five, display in the output.

Step 3: Allow to Execute break.sh 

Without permitting, you cannot run the bash script in the terminal. Thus, permit the file with the command:

chmod +x break.sh

Step 4: Execute the break.sh

Now, run the bash script file (break.sh) using the following manner:

./break.sh

When the bash script runs, it will print the first number which is divisible by 5, and then the loop will stop iterating. 

How to Use Continue When Working With Loops in Bash?

The example script below demonstrates the use of the Continue statement with loops in Bash. 

Step 1: Create a Bash Script File (continue.sh)

For a practical example, create a continue.sh file using Nano editor:

nano continue.sh

Step 2: Use Continue with Loop in Bash

Next, the following script can be used to easily understand the concept of Continue with loop in Bash script:

#!/bin/bash

# Find the numbers divisible by 5 in a list of numbers

numbers=(7 14 3 10 22 35 30 45 54 9)

echo "List of numbers: ${numbers[@]}"

# Loop through each number in the list
for num in "${numbers[@]}"; do
# Check if the number is not divisible by 5
if (( num % 5 != 0 )); then
    continue
fi
echo "Found number divisible by 5: $num"

Done

In this script, if the number is not divisible by 5, the loop skips the current iteration, moves to the next number, and prints all the numbers divisible by 5 only.

Step 3: Allow to Execute continue.sh

Use the chmod +x to allow executable permission to continue.sh file: 

chmod +x continue.sh

Step 4: Execute the continue.sh

Now, run the continue.sh file from the terminal:

./continue.sh

You will see the loop did not stop after finding the first number but iterated till it found all the numbers that are divisible by 5. 

Through this post, you have seen how you can use break and continue when working with loops in Bash script. 

Conclusion

In Bash loops, you can utilize the break and continue statements to handle specific cases. If you want to terminate the loop as soon as it finds the first match, use the break statement. On the other hand, you can use the continue statement to move to the next iteration until it finds all matches. 

You can control the flow of execution, especially in the loops through these two statements.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video