while Loop
In this page:
Basic while Loop
while condition; do ... done re-evaluates condition before every pass through the body, stopping as soon as it becomes false. The body is responsible for eventually making the condition false, usually by updating a counter or state variable.
Example: Basic while Loop
#!/bin/bash
count=1
while [ "$count" -le 5 ]; do
echo "Count is $count"
count=$((count + 1))
done
Login to try C/C++/Java/PHP code in the editor
Deliberate Infinite Loop with break
while true; do ... done runs forever since true always exits 0, which is a deliberate pattern for loops whose exit condition is easier to check partway through the body rather than at the top. A break statement inside then ends the loop when the right moment arrives.
Example: Deliberate Infinite Loop with break
#!/bin/bash
attempts=0
while true; do
attempts=$((attempts + 1))
echo "Attempt $attempts"
if [ "$attempts" -ge 3 ]; then
echo "Giving up after $attempts attempts"
break
fi
done
Login to try C/C++/Java/PHP code in the editor
while With a Numeric Condition
while can use (( )) directly as its condition for arithmetic comparisons, which reads more naturally than [ ] with -le/-lt for numeric loops. The loop stops the moment the arithmetic expression evaluates to zero (false).
Example: while With a Numeric Condition
#!/bin/bash
n=5
while (( n > 0 )); do
echo "n=$n"
(( n-- ))
done
Login to try C/C++/Java/PHP code in the editor
while With a Compound Condition
Just like if, a while loop's condition can combine multiple checks using [[ ]] with &&/||, letting the loop continue only while several things all remain true. This is useful for loops that should stop on the first of several possible ending conditions.
Example: while With a Compound Condition
#!/bin/bash
count=0
found=false
while [[ $count -lt 5 && $found = false ]]; do
count=$((count + 1))
echo "Checking item $count"
if [ "$count" -eq 3 ]; then
found=true
echo "Found it at item $count"
fi
done
Login to try C/C++/Java/PHP code in the editor
- Writing a condition that never becomes false because a variable used in it is never updated inside the loop body, producing an infinite loop.
- Assuming
whilechecks the condition once and then just keeps running the body forever; it actually re-checks the condition before every iteration, including the very first one. - Forgetting that a
while readloop consuming from a pipe runs in a subshell in some contexts, meaning variables set inside it won't be visible after the loop -- covered in more depth alongside reading command output.
- Syntax:
while condition; do ... done-- the condition is any command/test, checked before each iteration. - The loop body must eventually change something that affects the condition, or it will run forever.
while true; do ... doneis a common deliberate infinite loop, meant to be exited viabreakorexitfrom inside.- A
whileloop can use[ ],[[ ]],(( )), or any arbitrary command as its condition, exactly likeif.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: