
You can create Bash scripts to manage repetitive tasks efficiently by using loops like for
, while
, and until
. In some cases, you may need to exit a loop early, such as when a condition is met, an input is received, or further execution is unnecessary. The break
command lets you exit the loop immediately, giving you control over script flow.
This article explains how to use the break
command inside for
, while
, and until
loops in Bash, using clear and focused examples that demonstrate how loop control works in each case.
The Short Answer Version
Here’s a quick summary of how the break
command behaves in different loop types:
# For loop with break: Stops when n equals 3
for n in {1..5}; do
[ "$n" -eq 3 ] && break
echo "n = $n"
done
# While loop with break: Stops when user types "stop"
while true; do
read -p "Type something: " user_input
[ "$user_input" == "stop" ] && break
echo "You typed: $user_input"
done
# Until loop with break: Stops when index reaches 6
index=1
until [ "$index" -gt 10 ]; do
[ "$index" -eq 6 ] && break
echo "Index: $index"
((index++))
done
The next sections explain how to use the break
command inside each loop type in more detail.
Use the break Command in a for Loop
A for
loop is useful when you know how many times the loop should run. If a condition arises during execution that makes continuing unnecessary, such as reaching a specific value, you can use the break
command to exit the loop early.
Command Syntax
for variable in list; do
[condition] && break
done
Command Demonstration
This example prints numbers from 1 to 10 but exits early when the value reaches 5.
#!/bin/bash
echo "Beginning number loop..."
for num in {1..10}; do
echo "Current: $num"
if [ "$num" -eq 5 ]; then
echo "Reached target number. Leaving."
break
fi
done
echo "Loop finished."
Output:
Beginning number loop...
Current: 1
Current: 2
Current: 3
Current: 4
Current: 5
Reached target number. Leaving.
Loop finished.
Use the break Command in a while Loop
A while
loop is ideal when you don't know in advance how many times the loop should run. You can use the break
command to exit the loop based on user input or other dynamic conditions.
Command Syntax
while [condition]; do
[condition] && break
done
Command Demonstration
This example accepts user input repeatedly and exits the loop when the user types quit
.
#!/bin/bash
echo "Provide inputs (enter 'quit' to exit):"
while true; do
read -p "Input: " response
if [ "$response" == "quit" ]; then
echo "User requested to quit."
break
fi
echo "Received: $response"
done
echo "Exited while loop."
Output:
Provide inputs (enter 'quit' to exit):
Input: test
Received: test
Input: something
Received: something
Input: quit
User requested to quit.
Exited while loop.
Use the break Command in an until Loop
An until
loop runs as long as the given condition remains false. When a specific condition becomes true, such as reaching a limit, you can use the break
command to exit the loop early.
Command Syntax
until [condition]; do
[condition] && break
done
Command Demonstration
This example counts from 1 to 10 but exits the loop early when the counter reaches 5.
#!/bin/bash
counter=1
echo "Starting counter, will stop at five..."
until [ "$counter" -gt 10 ]; do
echo "Counter: $counter"
if [ "$counter" -eq 5 ]; then
echo "Breaking loop at counter 5"
break
fi
((counter++))
done
echo "Until loop complete."
Output:
Starting counter, will stop at five...
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Breaking loop at counter 5
Until loop complete.
Breaking Out of Nested Loops
When using nested loops, the break
command only exits the innermost loop by default. To exit multiple levels at once, use break N
, where N
is the number of nested loops to break out of.
Command Syntax
for outer in list; do
for inner in list; do
[condition] && break N
done
done
Command Demonstration
This example prints coordinates from two nested loops and exits both loops when y
equals 2.
#!/bin/bash
for x in {1..3}; do
for y in {1..3}; do
echo "x=$x, y=$y"
if [ "$y" -eq 2 ]; then
echo "Exiting both loops."
break 2
fi
done
done
echo "Nested loops done."
Output:
x=1, y=1
x=1, y=2
Exiting both loops.
Nested loops done.
Conclusion
In this article, you explored how the break
command works in Bash scripting. You learned how to use it to exit early from for
, while
, and until
loops based on specific conditions. You also saw how to break out of multiple nested loops using break N
to control complex flow logic.
No comments yet.