Signup/Sign In

Bash if..else Statement

Bash if-else statement or conditional statement is the most fundamental concept in any programming language that provides the facility to execute code conditionally. It is useful when we want to execute a piece of code only if certain conditions meet and execute some when the condition does not meet.

The if-else statement is used for this purpose and has several other forms such as if, if-else, if-elif-else, nested if, etc.

We will learn all these conditional flows in this article. So, let's get started.

if Statement

This is the simplest use of a conditional statement that executes only when the specified condition is true. The general syntax of this statement is.

if CONDITIONAL-COMMAND
then
  STATEMENTS
fi

The CONDITIONAL-COMMAND is a condition, and the if statement executes only when this condition is true. The then keyword is associate with the if and enclose the statements that execute when the if is true. Let's understand by an example.

Example: if Statement

In this example, first, a statement reads the user input and then validates that in the if conditional statement. if the user input is less than 20 then the if condition will execute.

#!/bin/bash

echo -n "Enter a number: "
read VAL

if [[ $VAL -lt 20 ]]
then
  echo "The value is less than 20."
fi

Save this code into a .sh file and execute by using the following command.

$ bash filename.sh

It will ask for the user input and if the input is less than 20 then the output will be:


The value is less than 20.

if-else Statement

This statement is an additional version of the if-statement and provides else statement that executes when the if statement is false. So, if you want to execute some statements when the if condition fails. The general syntax of this statement is:

if CONDITIONAL-COMMAND
then
  STATEMENTS
else
  STATEMENTS
fi

Example: if-else Statement

In this example, first, a statement reads the user input and then validates that in the if conditional statement. if the user input is less than 20 then the if condition will execute, if not, then the else statement executes.

#!/bin/bash

echo -n "Enter a number: "
read VAL

if [[ $VAL -lt 20 ]]
then
  echo "The value is less than 20."
else
  echo "The value is equal or greater than 20."
fi

It will ask for the user input and if the input is greater than or equal to 20 then else block will be executed and the output will be:


The value is equal or greater than 20.

if-elif-else Statement

This statement is used to test multiple conditions in a row and exit the control if any of the conditions is true. It is also known as the if-else ladder in some programming languages. The general syntax of this statement is:

if CONDITIONAL-COMMAND1
then
  STATEMENTS1
elif CONDITIONAL-COMMAND2
then
  STATEMENTS2
else
  STATEMENTS3
fi

The elif statements execute only when the if condition is false.

Example: if-elif-else Statement

Here, if the user enters 20 then the if condition fails and the control goes to elif condition where condition is true and the statement executes.

#!/bin/bash

echo -n "Enter a number: "
read VAL

if [[ $VAL -lt 20 ]]
then
  echo "The value is less than 20."
elif [[ $VAL -eq 20 ]]
then
  echo "The value is equal to 20."
else
  echo "The value is equal or greater than 20."
fi

It will ask for the user input and if the input is equal to 20 then elif block will be executed and the output will be:


The value is equal to 20.

Nested if Statement

Bash allows using any number of if statements inside an if statement that means we can use nested if statement to test multiple conditions. It is helpful when we have to check multiple conditions inside an if statement. The general syntax of this statement is:

if CONDITIONAL-COMMAND1
then
  if CONDITIONAL-COMMAND2
  then
     STATEMENTS1
  else
     STATEMENTS2
  fi
else
  STATEMENTS3 
fi

The nested if executes only if the firsts if condition fails.

Example: Nested if Statement

In this example, we are taking three input values from the user and test which one is smaller by using the nested-if statement.

#!/bin/bash

echo -n "Enter first number: "
read VAL1
echo -n "Enter second number: "
read VAL2
echo -n "Enter third number: "
read VAL3

if [[ $VAL1 -lt $VAL2 ]]
then
  if [[ $VAL1 -lt $VAL3 ]]
  then
    echo "$VAL1 is smallest Number"
  else
    echo "$VAL3 is smallest Number"
  fi
fi

It will ask for the user input and if the inputs are 20, 50, 40 then the output will be:


20 is smallest Number

Conclusion

The Bash script uses conditional statements such as if, if-else, if-elif, and nested if to create conditional programs that execute based on the specified condition. In this article, we understand the use of conditional statements with proper examples and use cases.



About the author:
Passionate about Linux and the coding community, and sharing my knowledge to help others succeed. Knowledgeable tech writer offering Linux tips and guides for the newest programming languages.