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.
while condition; do commands done until condition; do commands done
Try it
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.
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
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 lineto read lines.-rstops backslashes from being interpreted, andIFS=keeps leading and trailing whitespace. Without them, data gets silently altered. readfails 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/nullor read the file on another descriptor:while read -r -u 3 line; do ...; done 3< file. while trueorwhile :loops forever untilbreak,exitor a signal. Always include a sleep or a limit when polling.untilis clearer for "wait until it works" loops, avoiding a negated condition.while (( ... ))with arithmetic is faster than spawning[ ]orexpreach 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 withset -e; usei=$((i + 1))there.
Related
- for loops
- break and continue
- Pipelines
- Tutorial: Loops, Reading input