When working with Bash and shell scripting, you might need to use conditions in your script. In programming, conditions are crucial: they are used to assert whether some conditions are true or not.
In this article, we will learn one of the conditional statements that is if, elseif, else along with a few examples. In several other languages, the elif is written as “elseif” or “else if”. The elif statement helps us to make decisions among different choices.
It's crucial that you comprehend the fundamentals of BASH logical operators before you begin. Just look over the list below:
Please click here for more information.
Bash If Else Statement
In order to execute a Bash “if else” statement, you have to use four different keywords: if, then, else:
if : represents the condition that you want to check;
then : if the previous condition is true, then execute a specific command;
else : if the previous condition is false, then execute another command (note that the “ELSE” statement is optional, if you omit it, nothing will be executed if the condition evaluates to false.)
fi : closes the “if, then, else” statement.
When using the “if else” statement, you will need to enclose conditions in single or double squared brackets.
if [[ condition ]] then <execute command> else <execute another command> fi
Bash if conditionals can have different forms. The most basic if statement takes the following form:
if TEST-COMMAND then STATEMENTS fi
The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword. If the TEST-COMMAND evaluates to True, the STATEMENTS get executed. If TEST-COMMAND returns False, nothing happens; the STATEMENTS get ignored.
Let’s look at the following example script that checks whether a given number is greater than 100:
#!/bin/bash echo -n "Enter a number: " read VAR if [[ $VAR -gt 100 ]] then echo "The variable is greater than 100." fi
The Bash if..else statement takes the following form:
if TEST-COMMAND then STATEMENTS1 else STATEMENTS2 fi
If the TEST-COMMAND evaluates to True, the STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, the STATEMENTS2 will be executed. You can have only one else clause in the statement.
Let’s add an else clause to the previous example script:
#!/bin/bash read -p "Enter a number: " NUM echo if [ $NUM -gt 100 ] then echo "We have entered in IF block." sleep 3 echo "Your Number is greater than 100" echo date else echo "You have entered number less than 100." fi echo "Script execution completed successfully."
If you run the code and enter a number, the script will print a different message based on whether the number is greater or less/equal to 100.
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.
if [ condition1 ] then statement1 elif [ condition2 ] then statement2 elif [condition3 ] then statement3 else statement_n fi
Let’s add an elif clause to the previous script:
#!/bin/bash echo -n "Enter a number: " read VAR if [ $VAR -gt 10 ] then echo "The variable is greater than 10." elif [ $VAR -eq 10 ] then echo "The variable is equal to 10." else echo "The variable is less than 10." fi
We will count the number of network interfaces here:
#!/bin/bash value=$(ip addr show | grep -v LOOPBACK | grep -ic mtu) if [ $value -eq 1 ] then echo "1 Active Network Interface found." elif [ $value -gt 1 ] then echo "Found Multiple active Interface." else echo "No Active interface found." fi