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

Bash Scripting – Else If Statement

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.

Similar Reads

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....

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....

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....

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....