Bash while loop

while command executes a set of commands as long as the condition evaluates to a zero exit status. When the condition evaluates to non-zero exit status then while command stops executing the commands listed between do and done.

Syntax of Bash while loop

while [ condition ]
do
   commands
done

Example: To print multiplication table using while loop

#Bash Script to print multiplication table
read -p "Enter a number: " number
i=1
while [ $i -le 10 ]
do
  echo "$number x $i = $((number*i))"
  i=$((i+1))
done

Output of the above program

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50