EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

If Else in Shell Scripting

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Shell Scripting Tutorial » If Else in Shell Scripting

If Else in Shell Scripting

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

if else Statement chart

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

Popular Course in this category
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

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:

If Else in Shell Scripting Example 1

Output for False Condition:

If Else in Shell Scripting Example 2

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:

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 1 Example 3

Valid Condition 2:

Valid Condition 2 Example 4

Valid Condition 3:

Valid Condition 3 Example 5

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:

if_then_else_if_then_fi_fi Statement

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:

If Else in Shell Scripting Example 6

Valid Condition:

Valid Condition Example 7

Condition 1 is not valid:

Condition 1 is not valid Example

Note: In the above screenshot, if the first condition is not valid. The next condition will not validate even though the input “b=200” is correct.

Recommended Articles

This is a guide to If Else in Shell Scripting. Here we discuss the Introduction to If Else in Shell Scripting and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

  1. Variables in Shell Scripting
  2. While loop in Shell Scripting
  3. Loops in Shell Scripting
  4. Uses of Shell Scripting

Shell Scripting Training (4 Courses, 1 Project)

4 Online Courses

1 Hands-on Project

18+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Shell Scripting Tutorial
  • Shell Scripting Basics
    • What is Shell Scripting
    • Uses of Shell Scripting
    • Shell Scripting Commands
    • Shell Script Parameters
    • Variables in Shell Scripting
    • Shell Script Argument
    • Execute Shell Script
    • Shell Script Set Variable
    • Shell Script Operators
    • Function in Shell Scripting
    • Echo in Shell Scripting
    • if condition in shell script
    • If Statement in Shell Scripting
    • If Else in Shell Scripting
    • Switch Case in Shell Scripting
    • Shell Script Case
    • Loops in Shell Scripting
    • For Loop in Shell Scripting
    • While loop in Shell Scripting
    • Shell Script Set
    • Shell Script Sleep
    • Shell Script Usage
    • Eval in Shell Script
    • Shell Script Read File
    • What is Bash?
    • What is Bash Scripting
    • Batch Scripting Commands
    • Bash Script Variables
    • Bash Set Variables
    • Bash Local Variables
    • Bash Variable in String
    • Bash Concatenate Strings
    • Bash Export Variable
    • Bash Alias
    • Bash Trim String
    • Bash Replace String
    • Bash File
    • Shell Scripting Interview Questions

Related Courses

Shell Scripting Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Shell Scripting Training (4 Courses, 1 Project) Learn More