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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

12 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

12 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

12 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

12 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago