Bash if statements with examples

Bash if statements with examples

Bash conditional statements mainly used in shell programming in which we have to check the conditions. These statements evaluate the conditions whether the condition is true or false, if the conditions is true then first block of code is executes or if not then else part of code will be execute.

Bash conditional statements are of 4 types :

  • Simple if statement(simple if )
  • If-else statement (if else)
  • If-elif-else statement (Else if ladder)
  • Nested if statement (nested if)

1. Simple if statement(simple if ) : this is simple if statement in which a block of code executes which enclosed between keywords “then” and “fi”.

Syntax :

Code:
if [ condition ]
then
  -- Block of Commands --
Fi
For example :
Code:
#!/bin/bash
echo "Type an Even Number :"
read x
i=$(expr $x % 2)
if [ $i -ne 0 ]
then
echo "not Even!"
fi
output:

Code:
Type an Even Number :
7
Not Even
2. If-else statement : in this statement if the condition is true then “Block1” code is execute and if the condition is false then it jumps to Block2 or else part. If the condition is false then else part will be executed.

Syntax :

Code:
if [ condition ]
then
  -- Block1 --
else
 -- Block2 --
Fi
For example :
Code:
 #!/bin/bash

echo "Enter any Number :"
read x
i=$(expr $x % 2)
if [ $i -eq 0 ]
then
  echo "Number is Even!"
else
   echo "Number is Odd!"
fi
output :

Code:
Enter any Number :
3
Number is Odd!
Enter any Number :
12
Number is Even!
3. If-elif-else statement (Else if ladder) : this statement is helpful if you want to select one block of code from many blocks. In this case first condition1 is checked, if it “true” then “Block1” is executed and if condition 1 is false then condition 2 is checked. If condition2 is true then Block2 code is executed, but if it false then the it enters into else block and Block 3 code executes.

Syntax :

Code:
if [ condition1 ]
then
   -- Block1 --
elif [ condition2 ]
then
  -- Block2 --
else
  -- Block3 --

Fi
For example :
Code:
#!/bin/bash
echo "Type any Number :"
read number
if [ $number -lt 0 ]
then
echo "Negative!"
elif [ $number -eq 0 ]
then
  echo "Its Neither Positive Nor Negative!!"
else
echo "Positive!"
fi
Output :

Code:
Type any Number :
10
Its Positive!
Enter any Number :
-6
Its Negative!
Enter any Number :
0
Its Neither Positive Nor Negative!
4. Nested if statement (nested if) : In nested if statement “fi” is the keyword which shows end of the inner if statement. In nested statement there are more than if-then-else or if-elif-else statements are available. Lets see how the stamen will be executed:

Syntax:

Code:
if [ condition ]
then
  -- Commands --
else
   if [ condition ]
  then
  -- Block of Commands --
  fi
fi
For example :

Code:
#!/bin/bash
echo "b = $1500"
echo "Enter the Amount You want to Withdraw."
read amount
if [ $amount -gt 1500 ]
then
 echo "Insufficient Balance!"
else
   if [ $amount -gt 950 ]
  then
echo "Warning! Keep $50 in your account to keep it active!"
  fi
fi
Author
bhawanisingh
Views
1,655
First release
Last update
Rating
0.00 star(s) 0 ratings
Top