π Bash Loop Control β Using break and continue in Shell Scripts
π§² Introduction to Bash Loop Control β Manage Flow with break and continue
In Bash scripting, break and continue are control statements used inside loops to alter the normal flow of execution. They help you build smarter, more efficient loops by allowing early exits or skipping certain iterations based on specific conditions.
These tools are especially useful in for, while, and until loops when dealing with conditions like error checks, filtered data, or early termination.
π― In this article, youβll learn:
- How to use
breakto exit a loop prematurely - How to use
continueto skip the current loop iteration - Real-world examples for both loop control statements
- Best practices and common mistakes
π break β Exit a Loop Immediately
The break statement stops the execution of the current loop, skipping any remaining iterations.
π§ Syntax:
for item in list; do
if [[ condition ]]; then
break
fi
done
π§ͺ Example: Stop on a Specific Value
for num in 1 2 3 4 5; do
if [[ $num -eq 3 ]]; then
break
fi
echo "Number: $num"
done
β Output:
Number: 1
Number: 2
π‘ Once
numbecomes 3, the loop stops immediately.
βοΈ continue β Skip Current Iteration
The continue statement skips the rest of the loop body for the current iteration and jumps to the next one.
π§ Syntax:
while condition; do
if [[ condition ]]; then
continue
fi
# code that will be skipped if continue runs
done
π§ͺ Example: Skip Even Numbers
for i in {1..5}; do
if (( i % 2 == 0 )); then
continue
fi
echo "Odd: $i"
done
β Output:
Odd: 1
Odd: 3
Odd: 5
π§
continueensures theechois only run for odd numbers.
π break and continue in while Loops
π§ͺ break Example:
count=1
while true; do
echo "Count: $count"
((count++))
if [[ $count -gt 3 ]]; then
break
fi
done
β Output:
Count: 1
Count: 2
Count: 3
π§ͺ continue Example:
count=0
while [[ $count -lt 5 ]]; do
((count++))
if ((count == 3)); then
continue
fi
echo "Number: $count"
done
β Output:
Number: 1
Number: 2
Number: 4
Number: 5
β οΈ Number 3 is skipped because of
continue.
π§ Nested Loops and break n
In nested loops, you can exit multiple levels using break N, where N is the number of levels to break out of.
π§ͺ Example:
for i in {1..3}; do
for j in {1..3}; do
if [[ $j -eq 2 ]]; then
break 2 # exits both loops
fi
echo "$i, $j"
done
done
β Output:
1, 1
β Best Practices for Loop Control
| Practice | Benefit |
|---|---|
Use break to optimize performance | Avoid unnecessary iterations |
Use continue to filter input early | Cleaner, more efficient logic |
Avoid overusing break/continue | Keeps code readable and maintainable |
| Always test loop exit conditions | Prevent infinite loops |
π Summary β Bash Loop Control
Loop control commands like break and continue help you optimize your loops for performance, clarity, and precision. Use them to stop loops early or skip undesired iterations, making your scripts smarter and more efficient.
π Key Takeaways:
breakexits the current loop immediatelycontinueskips the current iteration and resumes the next- Use
break nto exit nested loops - Helpful in
for,while, anduntilconstructs
βοΈ Real-world Uses:
- Skipping invalid input during file parsing
- Exiting search loops once the result is found
- Skipping locked resources in automation scripts
β FAQ β Bash Loop Control
β What is the difference between break and continue in Bash?
β
break exits the loop completely, while continue skips the current iteration and continues the next cycle.
β Can I use break in a while or until loop?
β
Yes, break works in all loop types: for, while, and until.
β How do I exit multiple loops at once in Bash?
β
Use break N, where N is the number of loop levels you want to exit.
β Is continue always necessary?
β
No, but itβs useful when you want to skip part of the loop body conditionally without stopping the loop.
β Can I use continue in a while loop?
β
Absolutely. It behaves the sameβskips the rest of the iteration and resumes the next check.
Share Now :
