control-flow

while / until / do / done

Repeats the body while (or until) the condition command exits 0. 'while' loops while the condition succeeds; 'until' is the inverse and is idiomatic for polling until a service becomes ready.

while COMMAND; do ...; done   |   until COMMAND; do ...; done

Common flags / operators

Flag / Operator Purpose
do / done Delimits the loop body
while read Idiomatic line-by-line input consumption
while :; Infinite loop (: is the true builtin)
until Same shape as while but inverts the condition
break / continue Exit or skip to the next iteration

Examples

while read -r line; do echo "$line"; done < file.txt

Reads file.txt line by line; -r prevents backslash mangling.

while (( i < 10 )); do echo "$i"; ((i++)); done

Arithmetic-tested loop with in-place increment.

until curl -sf http://localhost:8080/health; do sleep 2; done

Poll a health endpoint every 2s until it returns 2xx.

while IFS=, read -r a b c; do echo "$a $b"; done < data.csv

CSV parsing: IFS=, splits on commas per iteration.

Gotcha

Piping into 'while' (cmd | while read ...) runs the loop in a subshell — variable changes inside are lost. Use process substitution 'while ... done < <(cmd)' to preserve them.

Related

← All Bash builtins · Linux commands