Loop Control
Loop control statements give you fine-grained control over loop execution. break exits a loop early, continue skips to the next iteration, exit terminates the entire script, and return exits a function. Understanding these control mechanisms is essential for writing robust, efficient scripts that can handle edge cases and terminate gracefully!
Break Statement
The break statement immediately exits the innermost loop, regardless of the loop's normal termination condition. It's useful when you've found what you're looking for or encountered an error condition.
Click Run to execute your code
# Basic break usage
for i in {1..10}; do
if [ $i -eq 5 ]; then
echo "Breaking at $i"
break
fi
echo "$i"
done
# Output:
# 1
# 2
# 3
# 4
# Breaking at 5
# Break in while loop
count=0
while true; do
count=$((count + 1))
if [ $count -ge 5 ]; then
break
fi
echo "Count: $count"
done
- Exits only the innermost loop
- Execution continues after the loop
- Can be used with all loop types (for, while, until)
- Often used with if statements inside loops
Continue Statement
The continue statement skips the rest of the current loop iteration and jumps to the next iteration. It's useful for skipping specific cases while continuing the loop.
# Continue example: Skip even numbers
for i in {1..10}; do
if [ $((i % 2)) -eq 0 ]; then
continue # Skip to next iteration
fi
echo "$i" # Only prints odd numbers
done
# Output:
# 1
# 3
# 5
# 7
# 9
# Continue in while loop
count=0
while [ $count -lt 10 ]; do
count=$((count + 1))
if [ $((count % 3)) -eq 0 ]; then
continue # Skip multiples of 3
fi
echo "Count: $count"
done
-
continue: Skip remaining code in current iteration, go to next iteration-
break: Exit the loop entirely, go to code after loop
Breaking Nested Loops
By default, break only exits the innermost loop. To break out of multiple nested loops, you need to use flags or restructure your code.
# Break only exits inner loop
for i in {1..3}; do
for j in {1..3}; do
if [ $j -eq 2 ]; then
break # Only breaks inner loop
fi
echo "i=$i, j=$j"
done
echo "After inner loop: i=$i"
done
# Output:
# i=1, j=1
# After inner loop: i=1
# i=2, j=1
# After inner loop: i=2
# i=3, j=1
# After inner loop: i=3
# Breaking outer loop using a flag
outer_done=false
for i in {1..3}; do
for j in {1..3}; do
if [ $i -eq 2 ] && [ $j -eq 2 ]; then
outer_done=true
break # Break inner loop
fi
echo "i=$i, j=$j"
done
if [ "$outer_done" = true ]; then
break # Break outer loop
fi
done
- Bash doesn't support labeled breaks like some languages
- Use boolean flags to break outer loops
- Consider restructuring code with functions if breaking becomes complex
- Functions can use return to exit early
Exit vs Return
exit and return are related but different: exit terminates the entire script, while return exits only the current function.
# Exit: Terminates entire script
echo "Before exit"
exit 0 # Script stops here, exit code 0
echo "This never executes"
# Return: Exits function only
function test_function() {
echo "Inside function"
return 0 # Exits function, returns to caller
echo "This never executes"
}
echo "Before function"
test_function
echo "After function" # This executes
# Exit codes
exit 0 # Success
exit 1 # General error
exit 2 # Misuse of shell command
# ... other codes
# Return codes (functions)
return 0 # Success
return 1 # Error
# Check with $?
| Statement | Scope | Use Case |
|---|---|---|
break |
Exits innermost loop | Early loop termination |
continue |
Skips current iteration | Skip specific cases in loop |
exit |
Terminates entire script | Fatal errors, script completion |
return |
Exits current function | Function early return, error handling |
Common Mistakes
1. Using exit instead of break in loops
# Wrong - exit terminates entire script
for i in {1..10}; do
if [ $i -eq 5 ]; then
exit 0 # Script ends here!
fi
echo "$i"
done
echo "This never executes"
# Correct - break exits loop only
for i in {1..10}; do
if [ $i -eq 5 ]; then
break # Only exits loop
fi
echo "$i"
done
echo "This executes"
2. Using return outside a function
# Wrong - return only works in functions
#!/bin/bash
if [ $# -eq 0 ]; then
return 1 # Error: can only return from function
fi
# Correct - use exit in main script
#!/bin/bash
if [ $# -eq 0 ]; then
exit 1 # Exit script with error
fi
3. Confusing continue and break
# Wrong - break exits loop when we want to skip
for i in {1..10}; do
if [ $((i % 2)) -eq 0 ]; then
break # Wrong! Exits loop entirely
fi
echo "$i"
done # Only prints 1
# Correct - continue skips to next iteration
for i in {1..10}; do
if [ $((i % 2)) -eq 0 ]; then
continue # Skips even numbers, continues loop
fi
echo "$i"
done # Prints all odd numbers
Exercise: Control Loop Execution
Task: Practice using loop control statements!
Requirements:
- Use break to exit a loop when a condition is met
- Use continue to skip specific iterations
- Create nested loops and break the outer loop using a flag
- Demonstrate exit vs return in a function
- Show how break and continue differ in behavior
Show Solution
#!/bin/bash
# Loop control exercises
# 1. Break example
echo "Break example (stops at 5):"
for i in {1..10}; do
if [ $i -eq 5 ]; then
echo "Breaking at $i"
break
fi
echo " $i"
done
# 2. Continue example
echo -e "\nContinue example (skips even):"
for i in {1..10}; do
if [ $((i % 2)) -eq 0 ]; then
continue
fi
echo " $i"
done
# 3. Breaking nested loops
echo -e "\nBreaking nested loops:"
outer_done=false
for i in {1..3}; do
for j in {1..3}; do
if [ $i -eq 2 ] && [ $j -eq 2 ]; then
echo " Breaking at i=$i, j=$j"
outer_done=true
break
fi
echo " i=$i, j=$j"
done
[ "$outer_done" = true ] && break
done
# 4. Exit vs Return
echo -e "\nExit vs Return:"
function test_return() {
echo " Inside function"
return 0
echo " This won't execute"
}
echo "Before function"
test_return
echo "After function (script continues)"
Summary
- break: Exits innermost loop immediately
- continue: Skips to next iteration of loop
- exit: Terminates entire script (use exit codes)
- return: Exits current function (function-only)
- Nested Loops: Use flags to break outer loops
- Loop Scope: break/continue only affect loops, not scripts
- Function Scope: return only works inside functions
- Exit Codes: Use appropriate exit codes (0=success, non-zero=error)
What's Next?
Perfect! You've mastered loop control statements. Next, we'll learn about Select Menus - interactive menu loops that let users choose from options. Select loops are perfect for creating user-friendly command-line interfaces!
Enjoying these tutorials?