Bash if..else Statement Tutorial In Linux

Linux Bash provides the if, if..else , if..elif..else statements in order to execute code according to the specific condition. By using these if statements we can branch the code execution easily. The if statements provide different cases where we examine all of them in this tutorial.

if Statement

The if statement can be used to check the condition in a single step. We can call this true or false. If the specified condition is true the if statement code block is executed. If the specified condition is false nothing is executed inside the if statement code block. The syntax of the single if statement is like below.

if CONDITION
then
   CODE_BLOCK
fi
  • The CONDITION is checked and if it is true the CODE_BLOCK is executed.
  • CODE_BLOCK is executed if the CONDTION is true. The CODE_BLOCK is located between then and fi statements after the if statement line.

In the following example, we check if the age value is equal to the “20”. If the age value is equal to the “20”.

#!/bin/bash

age=20

if [[ $age -eq 20 ]]
then
  echo "Age equals to 20."
fi

if..else Statement

The if..else statement is very useful to check a condition and execute code according to the condition result. If the condition results True the if statement code block is executed. If the condition results False the else statement code block is executed. The syntax of the if..else statement is like below.

if CONDITION
then
  IF_CODE_BLOCK
else
  ELSE_CODE_BLOCK
fi
  • The CONDITION is checked and if it it True the IF_CODE_BLOCK is executed. If the CONDITON is False the ELSE_CODE_BLOCK is executed.
  • IF_CODE_BLOCK is executed if the CONDITION is True. The IF_CODE_BLOCK is located between then and else statements.
  • ELSE_CODE_BLOCK is executed if the CONDITION is False. The ELSE_CODE_BLOCK is located between else and fi statements.
See also  Linux time Command Tutorial

In the following example, we check if the age value is equal to the “20” or not. If the age value is equal to 20 we print the message “Age equals to 20.”. If the age value is not equal to 20 we print the message “Age does not equal to 20.”.

#!/bin/bash

age=21

if [[ $age -eq 20 ]]
then
  echo "Age equals 20."
else
  echo "Age does not equal 20."
fi

if..elif..else Statement

The most complex and powerful usage for the if statements are using multiple conditions and checking them. The if..elif.else statement is used to check multiple conditions which are not related to each other. If one of the conditions is met the code block of this condition is executed and the if..elif..else statement ended. The syntax is like below. The elif statement can be used single or more times according to the scenario.

if CONDITION
then
  IF_CODE_BLOCK
elif ELIF_CONDITION
then
  ELIF_CODE_BLOCK
...
else
  ELSE_CODE_BLOCK
fi
  • The CONDITION is checked and if it True the IF_CODE_BLOCK is executed. If the CONDITION is False the ELSE_CODE_BLOCK is executed.
  • IF_CODE_BLOCK is executed if the CONDITION is True. The IF_CODE_BLOCK is located between then and else statements.
  • ELIF_CONDITION is checked and if it is True the ELIF_CODE_BLOCK is executed. If the ELIF_CONDITION is False next elif or else condition is executed.
  • ELIF_CODE_BLOCK is executed is the realted ELIF_CONDITION is True.
  • ELSE_CODE_BLOCK is executed if the CONDITION is False. The ELSE_CODE_BLOCK is located between else and fi statements.

By making an example it will be more clear to understand the usage of the if..elif..else statement. In the following example, we check if the age value is lower than 20 or higher than 20. If the age value is 20 the else statement is executed.

#!/bin/bash

age=21

if [[ $age -lt 20 ]]
then
  echo "Age is lower than 20."
elif [[ $age -gt 20 ]]
then
  echo "Age is higher than 20."
else
  echo "Age equals 20."
fi

Nested if Statements

The if statements can be nested for more complex condition checks. Multiple if and related statements can be put into another if and related statements. In the following example we use two variables named age and name to check in nested if statements. First, we check if the age variable is equal to the “20” and then in the nested if we check if the name is “ismail”.

#!/bin/bash

age=20

name="ismail"

if [[ $age -eq 20 ]]
then
  if [[ $name -eq "ismail" ]]
  then
    echo "İsmail age is 20."
  else
    echo "Persoen age is 20."
  fi
fi

Multiple Conditions for if Statements

One of the most powerful features of the if statement is using multiple conditions in a single if or elif statement. The && operator can be used to AND multiple conditions and || can be used to OR multiple conditions. In the following example, we check the age and city variables by using the && operator.

#!/bin/bash

age=20

city="Ankara"

if [[ $age -eq 20 ]]
then
  if [[ $name -eq "ismail" ]]
  then
    echo "İsmail age is 20."
  else
    echo "Persoen age is 20."
  fi
fi

Leave a Comment