How to Use if...else Statement in Bash

Suraj Joshi Feb 02, 2024
  1. if Statement in Bash
  2. if ... else Statement in Bash
  3. if...elif...else Statement in Bash
  4. Nested if Statement in Bash
How to Use if...else Statement in Bash

Conditional statements are prevalent for decision making in almost all of the programming languages. They allow the execution of a statement or statements only if a particular condition is satisfied. if ... else is used as a conditional statement in most programming languages. In Bash we also have if, if...elif...else, if ... else and nested if statements as conditional statements.

if Statement in Bash

Syntax of if Statement

if Test-Expression
then
  Statements
fi

In the above example, if the Test-Expression is True, the Statements are executed. fi keyword is used to end the if statement.

If the Test-Expression is not True, none of the Statements gets executed.

To make our code look more readable and organized, we can use either 4-space or 2-space indentation.

Example: if Statement in Bash

echo -n "Enter numnber : "
read n
 
rem=$(( $n % 2 ))
 
if [ $rem -eq 0 ]
then
  echo "$n is even number"
fi

Output:

Enter numnber : 4
4 is even number

It accepts a number from the user and gives the output only if the number is even number.

If the number is even, the remainder is zero when the number is divided by two. Therefore the test-expression is True so that the echo statement is executed.

if ... else Statement in Bash

Syntax of if ... else Statement

if Test-Expression
then
  Statements-1

else
  Statements-2
fi

In this example, if the Test-Expression is True, the Statements-1 is executed; otherwise, the Statements-2 is executed. To end the if ... else statement, fi keyword is used.

Example: if...else Statement in Bash

echo -n "Enter numnber : "
read n
 
rem=$(( $n % 2 ))
 
if [ $rem -eq 0 ]
then
  echo "$n is even number"
else
  echo "$n is odd number"
fi

Output:

Enter numnber : 5
4 is odd number

It accepts a number from the user and gives output depending on whether the input number is exactly divisible by 2 or not.

If the number is even, the remainder is zero when the number is divided by two; therefore, the test-expression is True and the statement echo "$n is even number" gets executed.

If the number is odd, the remainder is not zero; therefore, the test-expression is False, and the statement echo "$n is odd number" gets executed.

if...elif...else Statement in Bash

Syntax of if...elif...else Statement

if Test-Expression-1
then
  Statements-1
elif Test-Expression-2
then
  Statements-2

else
  Statements-3
fi

If the Test-Expression-1 is True, the Statements-1 is executed. Otherwise, if the Test-Expression-2 is True, the Statements-2 is executed.

If neither of the test expressions is True, the Statements-3 is executed.

We can have as many elif statements as we wish, and the else statement is optional.

Example: if...elif...else Statement in Bash

echo -n "Enter the value of a: "
read a

echo -n "Enter the value of b: "
read b

if [ $a -lt $b ]
then
   echo "a is less than b"
   
elif [ $a -gt $b ]
then
   echo "a is greater than b"

else
   echo "a is equal to b"
fi

Output:

Enter the value of a: 4
Enter the value of b: 4
a is equal to b

It accepts two numbers as the user’s input and prints the result based on which of the test-expression is true.

If a<b, the program prints a is less than b.

If a>b, the program prints a is greater then b.

If neither of the conditional statements is true, the program prints a is equal to b.

Nested if Statement in Bash

When an if statement is placed inside another if statement, it is called a nested if statement.

echo -n "Enter numnber : "
read a
 
rem=$(( $a % 2 ))
 
if [ $rem -eq 0 ]
then
  if [ $a -gt 10 ]
  then
    echo "$a is even number and greater than 10."
	
  else
    echo "$a is even number and less than 10."
  fi
else
  echo "$a is odd number"
fi

Output:

Enter numnber : 46
46 is even number and greater than 10.

It demonstrates the usage of the nested if statement. If the number is exactly divisible by 2 and greater than 10, echo "$a is even number and greater than 10." statement is executed.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn