How to Use if else Conditionals Statement in Bash Script

Channel: Linux
Abstract: if [ condition_command ] then command1 command2 …….. last_command else if [ condition_command2 ] then command1 command2 …….. last_command else command

In bash script if statement checks whether a condition is true or not. If so , the shell executes the block of code associated with the if statement. If the statement is not true , the shell jumps beyond the end of the if statement block & Continues on.

In this guide, we will learn how to use if, if else, If..elif..else..fi statements. If-else statement is also known as conditional statement in bash script.

if Statement 

Syntax: 

if [ condition_command ]
then
        command1
        command2
        ……..
        last_command
fi

Example:

In below bash script example, we are comparing two numbers using if condi statement.

#!/bin/bash
number=150
if [ $number -eq 150 ]
then
    echo "Number is 150"
fi
if-else Statement

In addition to the normal if statement , we can extend the if statement with an else block. The basic idea is that if the statement is true , then execute the if block. If the statement is false , then execute the else block. Here block is the set of commands.

Syntax :

if [ condition_command ]
then
       command1
       command2
       ……..
       last_command
else
       command1
       command2
       ……..
       last_command
fi

Example:

#!/bin/bash
number=150
if [ $number -gt 250 ]
then
    echo "Number is greater"
else
    echo "Number is smaller"
fi
If..elif..else Statement

In bash script, if you wish to apply multiple conditions using if statement then use ‘ if elif else’. In this type of conditional statement, if the first condition is met then code below it will be executed otherwise next if condition will checked and if it is not matched then commands mentioned below else statement will be executed. It’s syntax and example is shown below.

Syntax :

if [ condition_command ]
then
       command1
       command2
       ……..
       last_command
elif [ condition_command2 ]
then
        command1
        command2
        ……..
        last_command
else
command1
command2
……..
last_command
fi

Example :

#!/bin/bash
number=150
if [ $number -gt 300 ]
then
    echo "Number is greater"
elif [ $number -lt 300 ]
then
    echo "Number is Smaller"
else
    echo "Number is equal to actual value"
fi
Nested if statements

If statement and else statement can be nested in a bash script. The keyword ‘fi’ shows the end of the inner if statement and all if statement should end with the keyword ‘fi’.

Basic syntax of nested if is shown below :

if [ condition_command ]
then
        command1
        command2
        ……..
        last_command
else
if [ condition_command2 ]
then
        command1
        command2
        ……..
        last_command
else
        command1
        command2
         ……..
         last_command
      fi
fi

Example:

#!/bin/bash
number=150
if [ $number -eq 150 ]
then
   echo "Number is 150"
else
if [ $number -gt 150 ]
then
    echo "Number is greater"
else
    echo "'Number is smaller"
   fi
fi

Also Read16 Quick Cat Command Examples in Linux 

Ref From: linuxtechi

Related articles