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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

while COMMANDS; do ...; done

Parameters

ParameterDescription
break / continue Leave the loop / skip to the next iteration.
until Loop while the condition fails.

Examples

A counter loop
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.

Process lines, skipping empty ones
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.

Was this page helpful?