← Back to Bash Course | Chapter 4: Loops | Lesson 6 of 14

while Loop

A while loop repeats its body for as long as a given condition keeps being true, checking that condition fresh before every single pass.

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

bash
#!/bin/bash
count=1
while [ "$count" -le 5 ]; do
    echo "Count is $count"
    count=$((count + 1))
done

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

bash
#!/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

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

bash
#!/bin/bash
n=5
while (( n > 0 )); do
    echo "n=$n"
    (( n-- ))
done

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

bash
#!/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
Common Mistakes
  1. Writing a condition that never becomes false because a variable used in it is never updated inside the loop body, producing an infinite loop.
  2. Assuming while checks 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.
  3. Forgetting that a while read loop 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.
Chapter Summary
  • 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 ... done is a common deliberate infinite loop, meant to be exited via break or exit from inside.
  • A while loop can use [ ], [[ ]], (( )), or any arbitrary command as its condition, exactly like if.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.