Open In App

Bash Scripting – Else If Statement

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to write a bash script for the Else If statement.

Conditional statements: The statements that perform specific functions based on certain conditions are called conditional statements. In bash scripting, we have several conditional statements like IF, IF-ELSE, IF-ELSE-IF, etc. Every statement has its way of working and according to the need, we use them.

IF Statement

This statement is used when there is a need to check only conditions. If the condition founds to be true then the statement was written inside the if block will get executed.

Syntax:

if (condition)
then
 statement
fi

Code:

if [ 15 -gt 10 ]
then  
# If variable less than 10  
   echo "a is greater than 10"

fi 

This program will check the condition, whether 15 is greater than 10 or not. If 15 is greater than 10, the statement written inside the IF block will get printed on the screen.

Output:

a is greater than 10

IF-ELSE statement

As seen in the If statement, If the condition is true, the IF statement block gets executed but if the condition is false nothing is returned or executed. If we want the program to perform certain action after the IF statement condition is false, we use the ELSE statement after the If statement.

Syntax:

if [condition ]
then  
    If statement
else
    ELSE statement
fi 
  • If the condition is true: the IF statement will get executed.
  • If the condition is false: the ELSE statement will get executed.

Code:

if [ 5 -gt 10 ]
then  
# If variable less than 10  
   echo "number is greater than 10"
else
   echo "number is less than 10"
fi 

Output:

number is less than 10

ELIF (ELSE IF) statement

ELIF is the keyword used for the ELSE IF statement in bash scripting. If in a loop if more than two conditions exist which can not be solved only by using IF-ELSE statement then ELIF is used. Multiple ELIF conditions can be defined inside one if-else loop.

ELIF syntax:

if [ condition1 ]
then
       statement1
 elif [ condition2 ]
then
       statement2
 elif [condition3 ]
then
       statement3
else
      statement_n
fi

Code:

#!/bin/bash
 # Initializing the variable
 a=20
 if [ $a < 10 ] 
then  
      # If variable less than 10    
      echo "a is less than 10" 
elif [ $a < 25 ] 
then  
      # If variable less than 25  
      echo "a is less than 25" 
else   
     # If variable is greater than 25   
     echo "a is greater than 25"  
fi 

Output:

a is greater than 25

NESTED statements

If one or more than one conditional statement is written inside another statement, this is called nested statements like IF statements inside another IF statement.

Syntax (Nested IF):

If [condition]
then 
      if [condition_2]
      then 
            statement_1
      
      fi
fi

Example:

#!/bin/bash
#Initializing the variable

if [ 12 -gt 10 ]
then
   if [ 12 -gt 15]
   then
       echo "number is greater than 15"

   else
       echo "number is less than 15"
   fi
fi 

Output:

number is less than 15


Similar Reads

Bash Scripting - Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to a
10 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we print or echo some text on screen with the help of a t
2 min read
Bash Scripting - Working of Bash Scripting
Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and ot
6 min read
Bash Scripting - Bash Read Password without Echoing back
Generally, you can see on the login page, whenever we type a password either it show dot (•) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And in some cases, a dot or asterisk also may not appe
3 min read
Bash Scripting - Write Output of Bash Command into Log File
Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file. Let see how to write the output of the bash command into a log file.
4 min read
Bash Scripting - Difference between Zsh and Bash
A shell is an environment in which various commands can be executed, it provides an interface between the user and the UNIX system. Basically, a shell is a command-line interpreter which interprets the commands given by the user, it can also read the combination of such commands which is known as a shell script. The shell provides us with an enviro
2 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally three options:-n: It does not print the trailing n
2 min read
Bash Scripting - Case Statement
A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an edge over if-statements because it improves the r
8 min read
Bash Scripting - If Statement
Bash is an interpreter for command languages. It is a default command interpreter on most GNU/Linux systems and is widely available on various operating systems. The name is an abbreviation for Bourne-Again SHell. Scripting enables for the execution of instructions that would otherwise be executed one by one interactively.  In this article, we will
15 min read
Leap Year Program with Bash Shell Scripting in Windows
Accurately the Earth has 365.24 days per year, to correct the approximation and keep track of the Solar Calendar the idea of leap year has been introduced to Gregorian Calendar. A leap year comes once every four years. In contrast, it has 366 days with regular years, having 365 days. Mathematical approach to check:Every year is a Leap year if it is
1 min read
Article Tags :