The WHILE statement

Syntax

while condition ; do
    command
done

Discussion

  • command may be a single command or multiple commands, which are either placed on different lines or divided by ; on the same line.
  • The built-in break command exits from the loop. If a parameter n is provided, then it breaks out of n nested loops.
  • The built-in continue command skip the remaining instructions in the loop, resuming with the next iteration of the loop. If a parameter n in provided, then it skips n nested loops.
  • The until loop is its opposite, but it is rarely used.

Examples

cat file.txt | while read line; do
  echo "${line}"
done
while true; do
  echo "Hello,  Mars!"
  break
done

2020–07–16