If Else Statement in Bash Script with Examples

If else Statement in Bash Script with Examples

If else statement in bash scripting alters the control flow of the execution and it is used for decision making. In this article, we are going to show you some examples of if-else statement. We have used RHEL 9 but this works in other Linux distributions as well.

If statement

If else statement has different forms. Following is one of the basic form.

if CONDITION

then

     STATEMENTS

fi

First condition is tested. if it is true then the statements are executed. If the condition evaluates to false, the statements are ignored and nothing happens.

Consider the following example.

#!/bin/bash

echo -n “Enter a number”
read Number

if [[ $Number -gt 10 ]]

then

    echo "The Number is greater than 10."

fi

Save this script in a file sample.sh and close it. Open the terminal and execute the file.

bash sample.sh

The script will ask you to enter a number. Suppose you input 15 which is greater than 10. The clause inside then will be executed and ‘The Number is greater than 10’ will be displayed on the terminal.

If else statement

If else statement is bash script has following syntax:

if CONDITION

then

    STATEMENTS

else

   STATEMENTS

fi

If else statement starts with if keyword followed by condition. If the condition becomes true, the statements inside then are executed. If the condition becomes false the statements inside else are executed.

Consider the following example:

#!/bin/bash

echo -n “Enter a number”
read Number

if [[ $Number -gt 10 ]]

then

   echo "The Number is greater than 10."

else

  echo “The Number is less than or equal to 10.”

fi

Save the file and run it on the terminal. Script will read the input. Suppose you enter 5 which is less than 10, condition becomes false and the statement in else is executed. ‘The Number is less than or equal to 10’ is displayed on the terminal.

If elif else statement

If elif else statement has following syntax.

 
if CONDITION

then

    STATEMENTS

elif

CONDITION

then

    STATEMENTS

else

    STATEMENTS

fi

If elif else starts with if keyword followed by condition. If the condition evaluates to true, the statements inside then are executed. Otherwise, the next condition is checked, If it is true the statements inside then are execute. It the condition is false, statements inside else are executed.

Consider the following example.

#!/bin/bash

echo -n “Enter a number”
read Number

if [[ $Number -gt 10 ]]

then

   echo "The Number is greater than 10."

Elif [[ $Number -lt 10 ]]

then

  echo “The Number is less than 10.”

else

  echo “The Number is equal to 10.”

fi

Save the script in a file sample.sh and run the script on terminal. Suppose when you input 5, an if condition is checked, which is false. Statements inside then are not executed and then elif condition is checked. Elif condition is checked which is true therefore statement inside then is executed and ‘The Number is less than 10’ is displayed on the terminal.

Multiple conditions

You can use multiple conditions in if else statement with the help of logical operators OR, AND.

Consider the following example. We are finding the largest number among the three numbers with the help of && operator.

#!/bin/bash

echo -n “Enter First Number:”
read Number1
echo -n ‘Enter Scond Number:’
read Number2
echo -n ‘Enter Third Number:’
read Number3

If [[ $Number1 -gt $Number2 ]] && [[ $Number1 -gt $Number3 ]]

then

  echo “$Number1 is greater.”

elif [[ $Number2 -gt $Number1 ]] && [[ $Number2 -gt $Number3]]

then

  echo “$Number2 is greater.”

else

 echo “$Number3 is greater.”

fi

Save the file sample.sh and run it on terminal. Suppose we input 5, 56 and 57 when program asks for number. If condition checks if number 1 is greater than number 2, which is false so the program skip the then statement and move to elif condition. It checks if number 2 is greater than number 3 which is false, it skips the statement in then clause. It runs the statement in else clause and display ‘57 is largest’ on the terminal.

Nested if else statements

Bash supports the nested if else statement. We will demonstrate it with the help of following example.

#!/bin/bash

echo -n "Enter First number:"
read Number1

echo -n "Enter Second Number:"
read Number2

echo -n "Enter Third Number:"
read Number3

if [[ $Number1 -gt $Number2 ]]

then

  if [[ $Number1 -gt $Number3 ]]

  then

      echo "$Number1 is largest."

  fi

elif [[ $Number2 -gt $Number1 ]]

then

   if [[ $Number2 -gt $Number3 ]]

  then

      echo "$Number2 is largest."
  
  fi

else

  echo "$Number3 is largest."

fi

Suppose we input three numbers 5, 55, and 56. First if condition is false, control goes to elif and it checks the condition which is true because 55 is greater than 5. The statement inside then is executed and condition is tested which is false so the statement inside else is executed and displays ‘56 is largest’ on the terminal.

if-else-statement

Thank you for reading the article. If you liked it, please share and follow Linux World.