Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash while and until loops

Reference for Bash while and until loops: repeat while or until a command succeeds, read a file line by line safely, avoid pipe subshells and write infinite loops.

while repeats a block of commands as long as a condition command succeeds (exit status 0). until is its mirror image: it repeats until the condition succeeds. Both test before each pass, so the body may run zero times.

The most common use of while is reading input line by line with read, since read returns a failure status at end of file. Counters and polling loops (until ping ...) are the other typical uses.

Terminal
while condition; do
  commands
done

until condition; do
  commands
done

Try it

Bash Counters and until

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reading lines safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
while list-1; do list-2; done
until list-1; do list-2; done
Part Description
list-1 Condition. The status of its last command is tested. For while, 0 continues; for until, non-zero continues.
list-2 Body, run on every pass.
< file after done Redirects standard input for the whole loop, so read consumes the file.

Returns: the status of the last body command that ran, or 0 if the body never ran.

Usage notes

  • Use while IFS= read -r line to read lines. -r stops backslashes from being interpreted, and IFS= keeps leading and trailing whitespace. Without them, data gets silently altered.
  • read fails on a last line that has no trailing newline, although it still fills the variable. The || [[ -n $line ]] guard handles that case.
  • Redirect the file into the loop (done < file) instead of piping (cat file | while ...). A pipeline puts the loop in a subshell, so variables changed inside are lost. Use process substitution (done < <(command)) to read a command's output without the subshell.
  • Commands inside the loop that read standard input (ssh, cat, read) swallow the rest of the file. Give them </dev/null or read the file on another descriptor: while read -r -u 3 line; do ...; done 3< file.
  • while true or while : loops forever until break, exit or a signal. Always include a sleep or a limit when polling.
  • until is clearer for "wait until it works" loops, avoiding a negated condition.
  • while (( ... )) with arithmetic is faster than spawning [ ] or expr each time (in Bash [ ] and [[ ]] are builtins, so this matters mostly with external commands).
  • Loop counters are plain variables: ((i++)) returns status 1 when the old value was 0, which stops a script running with set -e; use i=$((i + 1)) there.