Updated April 10, 2023
Introduction to If Else in Shell Scripting
“If else” is a conditional or control statement in the programming language. This plays a different action or computation depending on whether the condition is true or false. It belongs to the ALGOL 60 family (Algorithmic Language 1960).
Types of If Else in Shell Scripting
Below are the types of If Else in Shell Scripting:
1. if-else Statement
“if-else” is dealing with two-part of the executions. If the “if-else “condition is “true” then the first group of statements will execute. If the condition is “false” then the second group of statements will execute.
Syntax of if-else in Shell Scripting:
if [ condition ]
then
Statement 1 (first Group)
else
Statement 2 (second Group)
fi
Flowchart:
Working of if else condition in shell scripting:
As per the above flow, the interpreter will execute the “if condition”. If the condition is true. Then the “Statement 1” will execute. If the condition is false then “Statement 2” will execute. In “if else condition/control statement”, anyhow it will execute the condition. Moreover, the condition is true or false.
Example:
Code:
#!/usr/bin/bash
echo "Enter the value of A : "
read a
echo "Enter the value of B : "
read b
if [ "$a" == "$b" ]
then
echo "A is equal B"
else
echo "A is not equal to B"
fi
Explanation:
Let consider, we have two variables “a” & “b”. The value of “a” & “b” will provide by the user. In “if_else” condition, we are validation the “a” & “b” variables if the value of “a” & “b” is equal then the condition is true. If the value of “a” & “b” is not equal then the condition is false. If the condition is true then the first group of commands/statements will execute. If the condition is false then the second group of commands/statements will execute.
Output for True Condition:
Output for False Condition:
2. if_elif_else_fi Statement
The “if_elif_else_fi” is also called the “Ladder If”. When we need to validate the multiple conditions in the single “if block”. Then we need to use “Ladder If”. From the multiple commands, whichever is true. The same true condition will execute otherwise else block will execute.
Syntax:
if [ condition 1 ]
then
Statement 1
elif [ condition 2 ]
then
Statement 2
elif [ condition 3 ]
then
Statement 3
else
Statement 4
fi
Flowchart:
Working of if_elif_else_fi statement (Ladder If) in Shell Scripting:
As per the above flow chart, we have added three conditions 1, 2 & 3. If the first condition is true then “Statement 1” will execute an interpreter will directly go to the normal program and execute the further program. But the interpreter will validate all the “f condition” whichever the true. The true condition will execute. If no one condition will true then the false statement (“Statement 4”) will execute.
Example:
Code:
#!/usr/bin/bash
echo "Enter the value of A : "
read a
echo "Enter the value of B : "
read b
if [ "$a" == "$b" ]
then
echo "A is equal to B"
elif [ "$a" -gt "$b" ]
then
echo "A is greater B"
elif [ "$a" -lt "$b" ]
then
echo "A is less than B"
els
echo "Invalid input"
fi
Explanation:
Let consider, we have two variables “a” & “b”. The value of “a” & “b” will provide from the user end. In the “Ladder if_else” condition, we are validating 3 different conditions. As per the user inputs whichever the true condition is. The same valid line of codes will execute. If all the 3 conditions will not valid then the else part of code will execute.
Valid Condition 1:
Valid Condition 2:
Valid Condition 3:
3. if_then_else_if_then_fi_fi Statement
It is useful to validate the multiple “if_else” conditions sequently. It is also known as the nested if conditions. If the condition is true then further / nested “if_else” condition will validate. If the condition will false, then the further “if_else” condition will not validate.
Syntax:
if [condition 1 ]
then
Statement 1
if [ condition 2 ]
then
Statement 2
else
Statement 3
fi
else
Statement 4
fi
Flowchart:
Working of if_then_else_if_then_fi_fi (Nested if) in Shell Scripting:
In nested if condition, we can validate the multiple “if-else” conditions. The interpreter will validate the first condition of the “if_else”. If the condition is true then the further “if_else” condition will validate.
While validating the conditions if the first “if_else” condition may false then the interpreter will out of the loop and execute the else group of commands. Without validating the nested “if_else” conditions.
Example:
Code:
#!/usr/bin/bash
echo "Enter the value of A : "
read a
echo "Enter the value of B : "
read b
if [ "$a" -gt 100 ]
then
echo "A is greater than 100"
if [ "$b" -gt 100 ]
then
echo "B is greater than 100"
else
echo "A is greater than 100 but B is less than 100"
fi
else
echo "A is less than 100"
fi
Explanation:
We have two variables “a” & “b”. In “Nested if-else”, we are validating the two conditions. If the first “if_else” condition will true then only the next “if_else” condition will validate. If the first “if_else” condition may false then the first condition of the “else” part will execute. The control/interpreter will not execute the further or nested “if_else” condition. If the first condition of “if_else” may false.
Valid Condition 1 but Condition 2 is not valid:
Valid Condition:
Condition 1 is not valid:
Recommended Articles
We hope that this EDUCBA information on “If Else in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.