Bash While Loop: Syntax, Examples, and Common Patterns

March 17, 2026

Bash While Loop: Syntax, Examples, and Common Patterns

March 17, 2026

The bash while loop runs a block of commands repeatedly as long as a condition is true. It's the go-to loop when you don't know in advance how many iterations you need — waiting for a condition to change, reading input, or retrying a failing operation. This guide covers all while loop patterns including infinite loops, the until variant, C-style arithmetic loops, and the single most common infinite loop bug beginners hit.

Condition Check true Execute body loop false exit while condition; do ... done
While loop flow: condition is checked before each iteration; loop exits when condition is false

1. Basic While Loop Syntax

bash
#!/bin/bash

# Counter loop: count from 1 to 5
count=1
while [[ $count -le 5 ]]; do
    echo "Count: $count"
    count=$(( count + 1 ))   # increment — NEVER forget this!
done

# Output:
# Count: 1
# Count: 2
# Count: 3
# Count: 4
# Count: 5

2. Infinite Loop with break

When you need to run until a condition inside the loop triggers exit, use while true with break:

bash
#!/bin/bash

# Wait for a file to appear (poll every 2 seconds)
while true; do
    if [[ -f "/tmp/ready.flag" ]]; then
        echo "Ready flag found — proceeding"
        break
    fi
    echo "Waiting..."
    sleep 2
done

# Interactive menu loop
while true; do
    echo "1) Start  2) Stop  3) Exit"
    read -r choice
    case "$choice" in
        1) echo "Starting..." ;;
        2) echo "Stopping..." ;;
        3) echo "Goodbye"; break ;;
        *) echo "Invalid choice" ;;
    esac
done

3. The until Loop

until is the inverse of while — it runs while the condition is false, stopping when it becomes true:

#!/bin/bash

# until: runs until the condition is TRUE (opposite of while)
count=1
until [[ $count -gt 5 ]]; do
    echo "Count: $count"
    (( count++ ))
done

# Same result as the while example above — use whichever reads more naturally
# "until count is greater than 5" vs "while count is less than or equal to 5"

# Practical: retry a command until it succeeds
until curl -s https://api.example.com/health > /dev/null; do
    echo "API not ready, retrying in 5s..."
    sleep 5
done
echo "API is up!"

4. C-Style Arithmetic While Loop

#!/bin/bash

# C-style with (( )) arithmetic evaluation
i=0
while (( i < 10 )); do
    echo "$i"
    (( i++ ))
done

# Multiple conditions
x=0; y=10
while (( x < 5 && y > 0 )); do
    echo "x=$x y=$y"
    (( x++, y-=2 ))
done

5. while with read (Reading Files and Input)

#!/bin/bash

# Read file line by line (most common while + read pattern)
while IFS= read -r line; do
    echo "Processing: $line"
done < /etc/hosts

# Read from a command's output
while IFS= read -r process; do
    echo "Killing: $process"
done < <(pgrep -f "old-process-name")

# Read from stdin interactively
echo "Enter lines (Ctrl+D to stop):"
while IFS= read -r line; do
    echo "You typed: $line"
done

6. break and continue

i=0
while (( i < 10 )); do
    (( i++ ))
    (( i % 2 == 0 )) && continue   # skip even numbers
    (( i > 7 )) && break            # stop after 7
    echo "$i"
done
# Output: 1 3 5 7

The Most Common Infinite Loop Bug

Forgetting to increment your counter creates an infinite loop that locks your terminal:

# BUG: count never changes — loops forever
count=1
while [[ $count -le 5 ]]; do
    echo "Count: $count"
    # Missing: count=$(( count + 1 ))
done

# Kill it: Ctrl+C
# Prevention: write the increment BEFORE filling in the loop body

For related looping patterns, see the bash read file line by line guide which uses while read extensively, and the bash for loop guide for when you know the iteration count in advance.

Summary

Use while [[ condition ]]; do ... done for condition-based loops, while true with break for indefinite loops, until for "keep going until success" patterns, and while IFS= read -r line for file/stream processing. Always double-check your increment to avoid infinite loops.