while loop

The while loop executes the commands between the do and done statements as long as the tested condition is true. Here is the syntax:

while [conditional-expression]
do
commands
done

Here is a simple example:

NUM=0
while [ $NUM -lt 5 ]
do
echo “The number is “$NUM”.”
let NUM=$NUM+1 # let is a Bash and Korn shell built-in command for math.
done

In the example above you can see that the while loop executes while the value of the NUM variable is less than 5:

linux while loop

Geek University 2022