How to Use Conditional Statements in Bash Script

In this guide, we will learn how to use conditional statements like if, if-else, If-elif-else, netsted if and case in the bash script. Conditional statements plays an important role in bash script as it helps to take decision based on the condition.

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

if Statement 

Syntax: 

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

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

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -lt 150 ]
then
echo "Number is $n"
fi

When we run this script, it will print the number if it is less than 150.

If-statement-Bash-Script-Example

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.

Syntax :

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

If we modify the above script and insert else block then it would look like below,

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
    echo "Number $n is greater than 150"
else
    echo "Number $n is smaller than 150"
fi

If we run the script and enter the number as 129 then it will execute else block as shown below,

If-else-statement-example-bash-script

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
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
    echo "Number $n is greater than 150"
elif [ $n -lt 150 ]
then
    echo "Number $n is smaller than 150"
else
    echo "Number $n is equal to 150"
fi

Script Execution output,

if-elif-else-statement-example-bash-script

Nested if Statement

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

When we modify above script to use nested-if statement then its code would look like below,

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
   echo "Number $n is greater than 150"
else
if [ $n -lt 150 ]
then
   echo "Number $n is smaller than 150"
else
  echo "Number $n is equal to 150"
  fi
fi

Above Script execution output,

Nested-if-statement-examples-bash-script

Case Statement

Case statement is similar to if statement with multiple elif. case statement in bash script expands the expression and then tries to to find the match with all the patterns. When a match is found then all the statements will be executed till the double semicolon (;;). In case it does not find the match then statements mentioned under *) pattern will be executed.

Syntax of Case Statement

case expression in 
    pattern1) 
       statements 
       ;; 
    pattern2) 
       statements 
       ;; 
    pattern3) 
       statements 
       ;; 
    pattern-n) 
       statements 
       ;;
    *) 
       statements 
       ;; 
esac

Example,

#!/bin/bash

echo "Which is Your Favorite Linux Distro?"
echo "Debian, Ubuntu, SUSE, RHEL, Arch"
read -p "Type Your Linux Distro:" OS

case $OS in
    Debian)
        echo "Most Stable Linux OS, good choice "
        ;;
    Ubuntu)
        echo "Best Linux OS for Desktop and Servers"
        ;;
    SUSE)
        echo "Top Linux OS for SAP Application"
        ;;
    Arch)
        echo "Flexible OS for experienced Linux users"
        ;;
    *)
        echo "Please Enter Correct OS from the list"
        ;;
esac

Script Execution output,

Case-Statement-Example-Bash-Script

Also Read16 Quick Cat Command Examples in Linux

2 thoughts on “How to Use Conditional Statements in Bash Script”

  1. I’d suggest you to surround variables with “” by default, because if $number isn’t declared or has a space in it, bash will throw a syntax error.

    Reply

Leave a Comment