Bash If-else statement Tutorial


The conditional statements in computer programming provide a way in which the computer takes a decision based on whether a condition is true or false. This concept is common among various programming languages.

In this article, you will learn bash if, if-else,if-elif-else and their nested statements.

Bash if statement

Using an if without else statement is the most basic way to conditionally execute the bash code.

The syntax of the if statement is given below.

if [ expression ]
then
   statement
fi

If the condition is true the code under if block will be executed otherwise it will move to the next statement in the bash script.

Example of if statement in Bash

You can see the usage of the if statement in the given example.

#!/bin/bash

echo -n "Enter a number: "
read myVAR

if [[ $myVAR -gt 25 ]]
then
  echo "The variable is greater than 25."
fi

Save this file with the .sh extension and then execute with the given command.

bash ex1.sh

Where ex1.sh is our file name. You can also read how to execute a shell script in Linux.

Now you will see the output as given below.

Since the entered number is greater the statements under if will be executed.

Bash if-else statement

In the syntax of the bash if-else statement, if the expression under if is true then statement1 will be executed otherwise statement2 will be executed.

if [ expression ]
then
   statement1
else
   statement2
fi

Example of if-else statement in Bash

#!/bin/bash
echo -n "Enter a number: "
read myVAR

if [[ $myVAR -gt 25 ]]
then
   echo "The variable is greater than 25."
else
   echo "The variable is equal or less than 25."
fi

Save this file with the .sh extension and then execute with the given command.

bash ex2.sh

This will display the given output.

Since entered number here is less than 25 so the condition will become false this will execute the statement under else condition.

Bash if-elif-else statement

The if-elif-else is used when you want to execute the code based on multiple conditions. The syntax of this type of statement is given below.

if [ expression 1 ]
then
   Statement1
elif [ expression 2 ]
then
   Statement2
else
   Statement3
fi

First, expression1 will be checked if it is true statment1 will be executed otherwise control will move to test expression2 if it is true statement2 will be executed otherwise the statement under else statement will be executed.

Example of If-elif-Else statement in Bash

#!/bin/bash

echo -n "Enter a number: "
read myVAR

if [[ $myVAR -gt 25 ]]
then
   echo "The variable is greater than 25."
elif [[ $myVAR -eq 25 ]]
then
   echo "The variable is equal to 25."
else
   echo "The variable is less than 25."
fi

Save this file with .sh extension and then execute it. You can test this code by taking different numbers in your terminal.

Nested if-elif-else statement in Bash

You can use ifif-else or if-elif-else statements under another if if-else or if-elif-else statement is called nesting. The indentation is used to separate these blocks.

For example –

#!/bin/bash

read -p "Enter the value of a:" a
read -p "Enter the value of b:" b
read -p "Enter the value of c:" c

if [ $a -gt $b ]
then
    if [ $a -gt $c ]
    then
        echo "a is greatest"
    else
        echo "c is greatest"
    fi
else
    if [ $b -gt $c ]
    then
        echo "b is greatest"
    else
 echo "c is greatest"
    fi
fi

You can test this example by saving it .sh extension and then executing it in your terminal.

Let’s say we say this file with the name ex4.sh then use the given command to execute it.

bash ex4.sh

Conclusion

Try each of these conditional statements on your system so that you get a proper understanding of how it is working. Now if you have a query or suggestion then leave it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.