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.
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
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.
The continue command skips the current loop cycle and moves to the next iteration. It does not stop the full loop.
Basic syntax:
continue
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.
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.
#!/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.
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.
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.
Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…
DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…
PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…
Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…
Apache Tomcat is an open-source web server and Java servlet container. It is one of the…
Keeping your Ubuntu system updated is one of the best ways to protect it. Security…