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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…