As in many other programming language Bash also have while loop for reapting task over and over as per requirement which can easily maintain by condition expression (that can provided in while loop itself)
in this post we will try to cover various kinds of while loop in bash scripts with some their syntax and examples which hope will help readers in automation task in bash script.

Syntax Bash while loop

while [ condition expression ]; do
commands
statements
done

we can easily understand with this syntax that loops only stop working in case condition statement inside while loop first line become false. But how it increment or decrement which will cause some change
during repeatation or irtration, for this we have to make add something in code, that could be count increment or decrement or some variable that itself change during code.Let see some examples
for same.

#!/bin/bash
read -p “please mentioned number for Table :” number
count=1
while [ $count -le 10 ];do
echo -n “$number X $count =”
echo “$number*$count”| bc
(( count++ ))
done
=====================================
        OutPut
#./bash_while
please mentioned number for Table :7
7 X 1 =7
7 X 2 =14
7 X 3 =21
7 X 4 =28
7 X 5 =35
7 X 6 =42
7 X 7 =49
7 X 8 =56
7 X 9 =63
7 X 10 =70

In Linux Bash automation while loop used many times for reading files line by line for some activity over file’s data. many times it used in one liner script as below.We can also
used this in script as well.

        One liner
cat /etc/passwd| while read line ; do echo $line| awk -F: ‘{print “User:”$1 ” Shell:” $7}’; done

        Script
#!/bin/bash
cat /etc/passwd| while read line
do
echo $line| awk -F: ‘{print “User:”$1 ” Shell:” $7}’;
done

We can also use while loop as infinite loop. Sometime we need to do run infinite loop for some continuous Linux administration tasks.

Syntax Bash while infinite loop

while true;
do
commands
statements
done
=====================================
        OutPut
while :
do
commands
statements
done

With this above mention code, we can easily able to call infinite loop with while

There are Break and continue statement in while, which call to continue or break loop in such a way that loop get iterate again with next value or just get exit.
Please read this post for same
Continue And Break Statement In Bash’s For And While Loop