πŸ”„ 3. Bash Control Flow
Estimated reading: 4 minutes 43 views

🐚 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 break to exit a loop prematurely
  • How to use continue to 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 num becomes 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

🧠 continue ensures the echo is 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

PracticeBenefit
Use break to optimize performanceAvoid unnecessary iterations
Use continue to filter input earlyCleaner, more efficient logic
Avoid overusing break/continueKeeps code readable and maintainable
Always test loop exit conditionsPrevent 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:

  • break exits the current loop immediately
  • continue skips the current iteration and resumes the next
  • Use break n to exit nested loops
  • Helpful in for, while, and until constructs

βš™οΈ 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Loop Control (break, continue)

Or Copy Link

CONTENTS
Scroll to Top