How To

Bash Break And Continue Commands Explained With Examples

Introduction

The Bash break and continue commands are used to control loops in Bash scripting. When you work with for, while, or until loops, sometimes you may want to stop the loop early or skip one loop cycle. This is where break and continue are useful.

If you are learning Bash scripting, understanding these two commands will help you write better automation scripts. They are commonly used in menu scripts, file processing, log analysis, cybersecurity automation, and user input validation.

What Is The Break Command In Bash?

The break command is used to stop a loop immediately. When Bash reaches break, it exits the loop and continues running the commands after the loop.

Basic syntax:

break

Bash Break Command Example

Create a new Bash script:

nano break-example.sh

Add the following code:

#!/bin/bashfor number in {1..10}do    if [[ $number -eq 5 ]]; then        echo "Number 5 found. Stopping loop."        break    fi    echo "Number: $number"doneecho "Loop finished"

Save and run the script:

chmod +x break-example.sh./break-example.sh

Output:

Number: 1Number: 2Number: 3Number: 4Number 5 found. Stopping loop.Loop finished

Here, the loop stops when the number becomes 5.

What Is The Continue Command In Bash?

The continue command skips the current loop cycle and moves to the next iteration. It does not stop the full loop.

Basic syntax:

continue

Bash Continue Command Example

Create another script:

nano continue-example.sh

Add the following code:

#!/bin/bashfor number in {1..5}do    if [[ $number -eq 3 ]]; then        echo "Skipping number 3"        continue    fi    echo "Number: $number"done

Run the script:

chmod +x continue-example.sh./continue-example.sh

Output:

Number: 1Number: 2Skipping number 3Number: 4Number: 5

In this example, Bash skips the command below continue when the number is 3.

Using Break In A While Loop

The break command also works with while loops.

#!/bin/bashwhile truedo    read -p "Type exit to stop: " input    if [[ "$input" == "exit" ]]; then        echo "Stopping script..."        break    fi    echo "You typed: $input"done

This script keeps running until the user types exit.

Cybersecurity Example: Stop When Suspicious File Is Found

#!/bin/bashfor file in *do    if [[ "$file" == "malware.sh" ]]; then        echo "Suspicious file found: $file"        break    fi    echo "Checking file: $file"done

This simple script checks files and stops when a suspicious filename is found.

Difference Between Break And Continue

The break command stops the loop completely. The continue command skips only the current loop cycle and continues with the next one.

Use break when you want to exit a loop early. Use continue when you want to ignore a specific value or condition but keep the loop running.

Conclusion

The Bash break and continue commands are useful for controlling loops. They make your scripts smarter by allowing you to stop loops early or skip unwanted items.

For beginners, learning break and continue is important because loops are widely used in Bash scripting. These commands are helpful for automation, file checking, user input handling, log analysis, and cybersecurity scripts.

Cyber Defence

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

3 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

4 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

9 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

10 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

11 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

12 hours ago