How to Use If Statement in Bash Scripting

In this guide, we will explain how to use if statement in bash scripting. In bash shell scripting, If statement can be used in form of if, if-else, If-elif-else, netsted if and case.

Bash scripting is a powerful tool for automating tasks in the Linux environment. One of the key elements of any programming or scripting language is conditional logic, and in Bash, this is achieved through if statements.

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 if statement block & continues on.

If Statement

Syntax: 

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

Bash provides logical operators to combine multiple conditions in if statements. The common logical operators are:

  • -eq: Equal
  • -ne: Not equal
  • -lt: Less than
  • -le: Less than or equal to
  • -gt: Greater than
  • -ge: Greater than or equal to

In following 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

String Comparison with if-else

In the following example, we are comparing two strings,

#!/bin/bash
string1=Debian
string2=RHEL

if [ "$string1" = "$string2" ]; then
   echo "The strings are equal."
else
   echo "The strings are not equal."
fi

File Existence Check with if-else

#!/bin/bash
file_path=/var/scripts/migratedb.sh

if [ -e "$file_path" ]; then
   echo "The file exists."
else
   echo "The file does not exist."
fi

In above example, we are checking whether a file “migratedb.sh” exists or not.

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-statement-bash-scripting-example

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 If Statement in Bash Scripting”

  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