while Linux Commands

What is Linux while Command?

Explanation

while COMMAND:

while command is a looping command. It is used to execute set of statements repeatedly till it satisfies the condition when we run a script or a program.

SYNTAX :


while [ condition ]
do
command1
command2
command3
done

In the above syntax, the command 1,2 and 3 are the statements to be executed till a specified condition is satisfied.

EXAMPLE:


  1. x=5 while [ $x -le 10 ] do
    echo '$x times'
    x=$(( $x + 1 )) done
    In the above example, the condition of while loop is to check x <= 10.
    The loop executes till the condition gets satisfied and stops when x becomes greater than 10.
    -le - stands for less than, use -gt for greater than.
    Output:
    5 times 6 times 7 times 8 times 9 times 10 times

Ask Questions

Ask Question