Bash

Bash Else if (elif)

Bash Else if (elif)

In this topic, we'll understand the way to use if-else statements in Bash scripts to urge our automated tasks completed.

Bash if-else statements are utilised to do conditional tasks inside the sequential flow of execution of our statements. Sometimes, we would like to process a selected set of statements if a condition is true, and another set of statements if it's false. To perform such sorts of actions, we will apply the if-else mechanism. we will apply the condition with the 'if statement'.

Bash If Else Syntax

A syntax of if-else statement in Bash Shell Scripting are often described  as below:

if [ condition ]; 

then 

 else 

 fi 

Important Points to recollect 

We can use a group of 1 or more conditions joined using conditional operators.

Else block commands includes a group of actions to perform when the condition is fake .

The semi-colon (;) after the conditional expression may be a must.

Check out the subsequent examples demonstrating the utilization of the if-else statement in Bash Script

Example1:

In this example we have mentioned two scenarios where in 1st scenario we have tried to find out that 100 is greater or lesser than 300.

#!/bin/bash  
  
#when the condition is true  
if [ 100 -gt 300 ];  
then  
  echo "100 is greater than 300."  
else  
  echo "100 is lesser than 300."  
fi  
  
#when the condition is false  
if [ 300 -gt 100 ];  
then  
  echo "300 is greater than 100."  
else  
  echo "300 is lesser than 100."  
fi  

Let’s see what output we get when we run this script.
Graphical user interface, text, email, website

Description automatically generated

Example 2:

In this instance, we described how to use various conditions with the if-else statement in Bash. We use bash rational operators to join various conditions.

#!/bin/bash  
  
# When condition is true  
# TRUE && FALSE || FALSE || TRUE  
if [[ 10 -gt 9 && 10 == 9 || 2 -lt 1 || 25 -gt 20 ]];  
then  
  echo "Given condition is true."  
else  
  echo "Given condition is false."  
fi  
  
# When condition is false  
#TRUE && FALSE || FALSE || TRUE  
if [[ 10 -gt 9 && 10 == 8 || 3 -gt 4 || 8 -gt 8 ]];  
then  
  echo "Given condition is true."  
else  
  echo "Given condition is not true."  
fi  

Output of the given code snippet: -
Text

Description automatically generated

Bash If Else Statement during a Single Line

We can write an entire 'if-else statement' alongside the commands during a single line. you would like to follow the given rules to use the if-else statement during a single line:

Utilize a semi-colon (;) at the top of statements in if and else blocks.

Utilize white spaces as a delimiter to add all the statements.

An example is given below demonstrating the way to use the if-else statement during a single line:

#!/bin/bash  
  
read -p "Enter a value:" value  
if [ $value -gt 27 ]; then echo "The value you typed is greater than 27."; else echo "The value you typed is not greater than 27."; fi  

Output of this Command is given below, and for the this command I have shown both the condition.
Text

Description automatically generated

Bash Nested If Else

Just like nested if statement, the if-else statement also can be used inside another if-else statement. it's called nested if-else in Bash scripting.

Following is an example explaining the way to make use of the nested if-else statement in Bash:

Here is the code for nested if else conditions.

#!/bin/bash  
  
read -p "Enter a value:" value  
if [ $value -gt 9 ]; # -gt to check if it's greater than or not  
then  
  if [ $value -lt 11 ];  # -lt for checking the value if it's lesser than or not
  then  
     echo "$value>9, $value<11"  
  else  
    echo "The value you typed is greater than 9."  
  fi  
else echo "The value you typed is not greater than 9."  
fi 


Text

Description automatically generated