How to Use Conditional Statements (if-else) in Bash Script

In this guide, we will cover how to use conditional statements in the bash script.

Linux users often write scripts to automate repetitive tasks. We can use the conditional statements within the scripts to make them more robust. The conditional statement executes the block of code when certain condition evaluates to true.

There are different forms of conditional statements such as – if, if-else, if-elif-else, and, nested if-else. In this beginner friendly guide, we will understand their usage with practical examples.

Setting up an Example

Let’s demonstrate the use of conditional statements using a simple script. We will write a script that accepts an integer as input and tells whether the provided number is equal to, greater than, or less than a hundred.

We will develop this script step by step. In the beginning, we will just check whether the number is equal to a hundred or not. In the later sections, we will check whether the number is greater or less than a hundred.

if Statement

The general syntax of the if statement is as follows:

if [[ condition ]]; then
    statement-1
    statement-2
    statement-3
    ...
    ...
    statement-N
fi

In the above example, statements from the if block gets executed when the condition evaluates to true.

Let’s check whether the number is equal to a hundred or not with the help of the if statement:

#! /bin/bash
read -p "Enter a number to compare it with 100: " input
if [[ $input -eq 100 ]]; then
    echo "Entered number is equal to 100"
fi

Now, execute the script and enter the value 100 as an input:

$ bash script.sh
Enter a number to compare it with 100: 100
Entered number is equal to 100

In the above example, the script gives the expected output.

Next, run the script again and provide the value 200 as an input:

$ bash script.sh 
Enter a number to compare it with 100: 200

Here, the script doesn’t give any output. In the next section, we will see how to handle this scenario.

Bash-Script-Without-Condition

if-else Statement

The general syntax of the if-else statement is as follows:

if [[ condition ]]; then
    statement-1
    statement-2
    ...
    ...
    statement-N
else
    statement-1
    statement-2
    ...
    ...
    statement-N
fi

In this case, statements from the else block get executed when the condition evaluates to false.

Let’s modify the script to display the message when the number is not equal to a hundred. Our modified script looks like this:

#! /bin/bash
read -p "Enter a number to compare it with 100: " input
if [[ $input -eq 100 ]]; then
    echo "Entered number is equal to 100"
else
    echo "Entered number is not equal to 100"
fi

Now, execute the script and provide the value 200 as an input:

$ bash script.sh 
Enter a number to compare it with 100: 200
Entered number is not equal to 100

In the above example, the script gives the expected output.

If-else-Statement-Bash-Script-Linux

if-elif-else Statement

The general syntax of the if-elif-else statement is as follows:

if [[ condition ]]; then
    statement-1
    statement-2
    ...
    ...
    statement-N
elif [[ condition ]]; then
    statement-1
    statement-2
    ...
    ...
    statement-N
else
    statement-1
    statement-2
    ...
    ...
    statement-N   
fi

In this case, statements from the else block get executed when the first two conditions evaluate to false.

Let’s modify the script to identify whether the entered number is greater or less than a hundred. Our modified script looks like this:

#! /bin/bash
read -p "Enter a number to compare it with 100: " input
if [[ $input -eq 100 ]]; then
    echo "Entered number is equal to 100"
elif [[ $input -gt 100 ]]; then
    echo "Entered number is greater than 100"
else
    echo "Entered number is less than 100"
fi

Now, run the script and provide the value 200 as an input:

$ bash script.sh 
Enter a number to compare it with 100: 200
Entered number is greater than 100

Here, we can see that the script works as expected.

Next, run the script again and provide the value 50 value as an input:

$ bash script.sh 
Enter a number to compare it with 100: 50
Entered number is less than 100

In the above output, we can see that the script gives the expected output.

If-elif-else-statement-bash-linux

Nested if-else Statement

Bash allows us to use the conditional statement in the nested format. Let’s modify the previous script using the nested if-else statements.

Our modified script looks like this:

$ cat script.sh 
#! /bin/bash
read -p "Enter a number to compare it with 100: " input
if [[ $input -eq 100 ]]; then
    echo "Entered number is equal to 100"
else
    if [[ $input -gt 100 ]]; then
        echo "Entered number is greater than 100"
    else
        echo "Entered number is less than 100"
    fi
fi

In the above example, we used nested if-else statements in the else part.

Now, let’s run the script and verify that it works as expected.

First, provide the value which is greater than a hundred:

$ bash script.sh 
Enter a number to compare it with 100: 200 
Entered number is greater than 100

Next, provide the input value which is less than a hundred:

$ bash script.sh 
Enter a number to compare it with 100: 50
Entered number is less than 100

Nested-if-statement-bash-linux

Conditional Operators

Bash supports logical AND(&&) and OR(||) operators. We can use these operators as if and else statements.

Let’s check whether the number is equal to a hundred or not using the logical AND(&&) operator:

#! /bin/bash
read -p "Enter a number to compare it with 100: " input
[[ $input -eq 100 ]] && echo "Entered number is equal to 100"

Now, execute the script and check the output:

$ bash script.sh 
Enter a number to compare it with 100: 100
Entered number is equal to 100

In a similar way, we can use the logical OR(||) operator as an else statement. Our modified script looks like this:

#! /bin/bash
read -p "Enter a number to compare it with 100: " input
[[ $input -eq 100 ]] && echo "Entered number is equal to 100" || echo "Entered number is not equal to 100"

Let’s provide the value 200 as an input and observe the result:

$ bash script.sh 
Enter a number to compare it with 100: 200
Entered number is not equal to 100

Condtional-Operator-Bash-Linux

Conclusion

In this guide, we discussed how to use various conditional statements in bash. First, we saw the usage of the if statement. Next, we saw how to use an if-else statement. Then we saw how to use the elif and nested if-else statement. Finally, we saw how we can use conditional operators instead of if-else statements.

Read Also : How To Use Variables in Bash Script (Simple Guide)

Leave a Comment

8 + 15 =