Conditional Statements: There are total 5 conditional statements which can be used in bash programming
if statement
if-else statement
if..elif..else..fi statement (Else If ladder)
if..then..else..if..then..fi..fi..(Nested if)
switch statement




Here are some useful examples of if-else in shell scripts to give you a better idea of how to use this tool.
Command | Description |
&& | Logical AND |
$0 | Argument 0 i.e. the command that’s used to run the script |
$1 | First argument (change number to access further arguments) |
-eq | Equality check |
-ne | Inequality check |
-lt | Less Than |
-le | Less Than or Equal |
-gt | Greater Than |
-ge | Greater Than or Equal |
# ############################################ | |
# Program - 1 - Format | |
# ############################################ | |
if [condition] | |
then | |
statement1 | |
else | |
statement2 | |
fi | |
# ############################################ | |
# Program - 2 - Using if-else to check whether two numbers are equal | |
# ############################################ | |
#!/bin/bash | |
m=1 | |
n=2 | |
if [ $n -eq $m ] | |
then | |
echo "Both variables are the same" | |
else | |
echo "Both variables are different" | |
fi | |
# ############################################ | |
# Program - 3 - Using if-else to compare two values | |
# ############################################ | |
#!/bin/bash | |
a=2 | |
b=7 | |
if [ $a -ge $b ] | |
then | |
echo "The variable 'a' is greater than the variable 'b'." | |
else | |
echo "The variable 'b' is greater than the variable 'a'." | |
fi | |
# ############################################ | |
# Program - 4 - Using if-else to check whether a number is even | |
# ############################################ | |
#!/bin/bash | |
n=10 | |
if [ $((n%2))==0 ] | |
then | |
echo "The number is even." | |
else | |
echo "The number is odd." | |
fi | |
# ############################################ | |
# Program - 5 - Using if-else as a simple password prompt | |
# ############################################ | |
#!/bin/bash | |
echo "Enter password" | |
read pass | |
if [ $pass="password" ] | |
then | |
echo "The password is correct." | |
else | |
echo "The password is incorrect, try again." | |
fi | |
# ############################################ | |
# Program - 6 - if-elif-if.sh | |
# ############################################ | |
#!/bin/bash | |
#Purpose: Find biggest Number among 4 digits | |
# START # | |
echo -e "Please Enter a Value: \c" | |
read -r a | |
echo -e "Please Enter b Value: \c" | |
read -r b | |
echo -e "Please Enter c Value: \c" | |
read -r c | |
echo -e "Please Enter d Value: \c" | |
read -r d | |
if [ $a -gt $b -a $a -gt $c -a $a -gt $d ]; then | |
echo "$a a is big" | |
elif [ $b -gt $c -a $b -gt $d ]; then | |
echo "$b b is big" | |
elif [ $c -gt $d ]; then | |
echo "$c c is big" | |
else | |
echo "$d d is big" | |
fi | |
# END # | |
# ############################################ | |
# Program - 7 - if-elif-if.sh | |
# ############################################ | |
#!/bin/bash | |
#Purpose: If else statement example | |
# START # | |
echo -e "Enter any value: \c" | |
read -r a | |
echo -e "Enter any value: \c" | |
read -r b | |
if [ $a -gt $b ]; then | |
echo "$a is greater than $b" | |
else | |
echo "$b is greater than $a" | |
fi | |
# END # | |
# ############################################ | |
# Program - 8 - if-elif-if.sh | |
# ############################################ | |
#!/bin/bash | |
#Purpose: If statement example | |
# START # | |
echo -e "Please provide Value below ten: \c" | |
read -r value | |
if [ $value -le 10 ] | |
then | |
echo "You provided value is $value" | |
touch /tmp/test{1..100}.txt | |
echo "Script completed successfully" | |
fi | |
# ############################################ | |
# Program - 9 - Nested If | |
# ############################################ | |
#!/bin/bash | |
#Purpose: Validate and report Student subject marks | |
echo -e "Please Enter Maths Marks: \c" | |
read -r m | |
echo -e "Please Enter Physics Marks: \c" | |
read -r p | |
echo -e "Please Enter Chemistry Marks: \c" | |
read -r c | |
if [ $m -ge 35 -a $p -ge 35 -a $c -ge 35 ]; then | |
total=`expr $m + $p + $c` | |
avg=`expr $total / 3` | |
echo "Total Marks = $total" | |
echo "Average Marks = $avg" | |
if [ $avg -ge 75 ]; then | |
echo "Congrats you got Distinction" | |
elif [ $avg -ge 60 -a $avg -lt 75 ]; then | |
echo "Congrats you got First Class" | |
elif [ $avg -ge 50 -a $avg -lt 60 ]; then | |
echo "You got second class" | |
elif [ $avg -ge 35 -a $avg -lt 50 ]; then | |
echo "You Got Third Class" | |
fi | |
else | |
echo "Sorry You Failed" | |
fi | |
# 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