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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

2 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

2 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

2 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

2 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

2 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

3 days ago