Bash 5 reference · Shell Builtins and Syntax
while (Bash builtin)
Repeat commands while a condition succeeds; with read, it's the standard way to process input line by line.
Bash 5
Syntax
while COMMANDS; do ...; done
Parameters
| Parameter | Description |
|---|---|
break / continue |
Leave the loop / skip to the next iteration. |
until |
Loop while the condition fails. |
Examples
n=0
while (( n < 3 )); do (( n++ )); echo "try $n"; done
Output
try 1 try 2 try 3
Run this on your computer — see “Get Started” for setup.
printf 'a\n\nb\n' | while IFS= read -r line; do [[ -z $line ]] && continue; echo "got $line"; done
Output
got a got b
Run this on your computer — see “Get Started” for setup.
Related
for· Loop over words, files, ranges or array elements, or use the C-style form for counters.read· Read a line from standard input into variables, splitting on IFS; use -r to keep backslashes and -p for a prompt.- Bash Loops · tutorial