The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.
Most languages have the concept of loops: If we want to repeat a task twenty times, we don’t want to have to type in the code twenty times, with maybe a slight change each time.
As a result, we have for and while loops in the Bourne shell.
Types of Loops in Shell Scripting

Syntax of Shell Scripting

Control Flow of While Loop

Example of While Loop in Shell Script

Boolean Operators When Working With Shell Scripts
Apart from the boolean operators that you’ve seen in other programming languages, including ==, <, >, <=, >=, etc., these are shell script specific operators. The major usage difference is that these operators will only work when numeric values are provided. With string value, the operation will simply fail.
Operator | Description | Example |
---|---|---|
-ne | Checks if both operands are not equal | [ 1 -ne 2 ] is TRUE |
–eq | Checks if both the operands are equal | [ 1 -eq 2 ] is FALSE |
–lt | Check if the left operand is lesser than the right operand | [1-lt 2 ] is TRUE |
–gt | Check if the left operand is greater than the right operand | [1-gt 2 ] is FALSE |
–le | Check if the left operand is less than or equal to the right. | [1-le 2 ] is TRUE |
–ge | Check if the left operand is greater than or equal to the right. | [1-ge 2 ] is FALSE |
Creating a While Loop in Shell Script
A while loop doesn’t have a repetition limit by default. We have to explicitly provide it a condition that at some point during the code execution, will turn to false. Else you’ll have a loop that doesn’t end unless an interrupt signal is sent.

Save the script above by any name with the .sh extension. To run the file, you can either run it with the bash command like bash <filename>.sh or make the file executable using the chmod command. To make the script executable, run chmod +x <filename>.sh and then ./<filename>.sh will allow you to run the script.
Let’s understand the script line by line.
- i=0 – Here we set the variable $i to 0. Learn more about variables in our previous tutorial
- while [ $i -le 10 ] – Run the while loop only until the variable $i is lesser than or equal to 10. So this loop will run 11 times including the ZERO-th run.
- do – Marks the beginning of the while loop in shell scripts
- echo True – A simple command that will print the word “True” on our terminal
- ((i++)) – C style variable increments to add 1 to the variable i with every loop. There are various other ways to perform an increment include $i=((i+1)), ((i=i+1)), etc. Feel free to use either one.
- done – Marks the end of the while loop
- echo False – This again simply prints out the word False on the screen to indicate that condition has now turned false.
Example of nested While Loop

# ############################ | |
# Program - 1 | |
# ############################ | |
#!/bin/sh | |
a=0 | |
while [ $a -lt 10 ] | |
do | |
echo $a | |
a=`expr $a + 1` | |
done | |
# ############################ | |
# Program - 2 | |
# ############################ | |
#!/bin/bash | |
x=1 | |
while [ $x -le 5 ] | |
do | |
echo "Welcome $x times" | |
x=$(( $x + 1 )) | |
done | |
# ############################ | |
# Program - 3 | |
# ############################ | |
# And here is above code as a bash while one liner: | |
x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done | |
# ############################ | |
# Program - 4 | |
# ############################ | |
# Here is a sample shell code to calculate factorial using while loop: | |
#!/bin/bash | |
counter=$1 | |
factorial=1 | |
while [ $counter -gt 0 ] | |
do | |
factorial=$(( $factorial * $counter )) | |
counter=$(( $counter - 1 )) | |
done | |
echo "$factorial" | |
# ############################ | |
# Program - 5 | |
# ############################ | |
# While loops are frequently used for reading data line by line from file: | |
#!/bin/bash | |
FILE=$1 | |
# read $FILE using the file descriptors | |
exec 3<&0 | |
exec 0<$FILE | |
while read line | |
do | |
# use $line variable to process line | |
echo $line | |
done | |
exec 0<&3 | |
# ############################ | |
# Program - 6 | |
# ############################ | |
# Here is a bash while loop that read those IP address separated by Internal Field Separator ($IFS) to an octothorpe (#): | |
#!/bin/bash | |
# Script name: block_ips.sh | |
# Set the Internal Field Separator to an octothorpe '#' | |
IFS='#' | |
# Set input file name here | |
INPUT="bad-guys.ips.txt" | |
# Read file line-by-line to get an IP and comment to block it using the iptables | |
while read -r ip comment | |
do | |
/sbin/iptables -A INPUT -s "$ip" -m comment --comment "$comment" -j DROP | |
done < "$INPUT" | |
# ############################ | |
# Program - 7 | |
# ############################ | |
# Infinite for while can be created with empty expressions, such as: | |
#!/bin/bash | |
while : | |
do | |
echo "infinite loops [ hit CTRL+C to stop]" | |
done | |
# ############################ | |
# Program - 8 | |
# ############################ | |
# Conditional while loop exit with break statement | |
#!/bin/bash | |
while : | |
do | |
read -p "Enter two numnbers ( - 1 to quit ) : " a b | |
if [ $a -eq -1 ] | |
then | |
break | |
fi | |
ans=$(( a + b )) | |
echo $ans | |
done | |
# ############################ | |
# Program -9 | |
# ############################ | |
# Early continuation with the continue statement | |
while [ condition ] | |
do | |
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any. | |
statements2 | |
if (condition) | |
then | |
continue #Go to next iteration of I in the loop and skip statements3 | |
fi | |
statements3 | |
done | |
# ############################ | |
# Program -10 | |
# ############################ | |
#!/bin/ksh | |
# Script name: while.ksh | |
num=1 | |
while (( num < 6 )) | |
do | |
print "The value of num is: $num" | |
(( num = num + 1 )) # let num=num+1 | |
done | |
print "Done." | |
# ############################ | |
# Program -11 | |
# ############################ | |
#!/bin/ksh | |
# Script name: readinput.ksh | |
print -n "Enter a string: " | |
while read var | |
do | |
print "Keyboard input is: $var" | |
print -n "\nEnter a string: " | |
done | |
print "End of input." | |
# ############################ | |
# Program -12 | |
# ############################ | |
#!/bin/ksh | |
# Script name: internalredir.ksh | |
# set the Internal Field Separator to a colon | |
IFS=: | |
while read name number | |
do | |
print "The phone number for $name is $number" | |
done < phonelist | |
# ############################ | |
# Program -13 | |
# ############################ | |
a=1 | |
until [ $a -ge 3 ] | |
do | |
echo “value of a=” $a | |
a=`expr $a + 1` | |
done | |
# ############################ | |
# Program -14 | |
# ############################ | |
num=1 | |
while [ $num -le 5 ] | |
do | |
read var | |
if [ $var -lt 0 ] | |
then | |
break | |
fi | |
num=`expr $num + 1` | |
done | |
echo “The loop breaks for negative numbers” | |
# ############################ | |
# Program -15 | |
# ############################ | |
#!/bin/bash | |
# While Loop Example with 2 table, print any given number table. | |
# See Full Explanation of this above shell script [while loop](https://www.youtube.com/Techarkit?sub_confirmation=1) | |
#START | |
echo -e "Please provide one value: \c" | |
read -r c | |
i=1 | |
while [ $i -le 10 ] | |
do | |
b=`expr $c \* $i` | |
echo "$c * $i = $b" | |
i=`expr $i + 1` | |
done | |
#END | |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND