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.