Site icon DesignLinux

Bash if..else Statement

Bash if..else Statement

The if statement is the most basic concepts of any programming language. In Bash, you can also use if…else, if…elif.else and nested if statements like other programming languages. In this article, we will show you the basics of the Bash if statement and use of it in your shell scripts.

if Statement

If statement can be used in different forms but the following the most basic form:

if TEST-COMMAND
then
  STATEMENTS
fi

The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.

Here, if the result of TEST-COMMAND to True, the STATEMENTS will be execute. If TEST-COMMAND becomes False, the STATEMENTS gets ignored.

It’s a best practice to indent the code and code blocks with blank line. Indentations and blank lines make your code more readable and organized.

Following is the example of basic script to check the number is greater than 100:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The entered number is greater than 100."
fi

Save and close the file. Run it from the command line:

bash example.sh

It will ask you to enter any number. If you will enter 105, the condition becomes true so it will execute the echo command which is given inside.

The entered number is greater than 100.

if..else Statement

Following is the form of Bash if..else statement:

if TEST-COMMAND
then
  STATEMENTS1
else
  STATEMENTS2
fi

If the result of TEST-COMMAND to True, it will execute the STATEMENTS1. If TEST-COMMAND returns False, the STATEMENTS2 will be execute. You can have only one else clause in the statement.

For example, we will make change in previous example by adding else:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The entered number is greater than 100."
else
  echo "The entered number is equal or less than 100."
fi

Save the file and run again same as previously. Once you enter the number, the script will display a different message based on whether the number is greater or less/equal to 100.

if..elif..else Statement

The Bash if..elif..else statement takes the following form:

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

If the TEST-COMMAND1 returns True, it will execute the STATEMENTS1. Otherwise it will check the TEST-COMMAND2 and if it returns True, the STATEMENTS2 will be execute. If none of any TEST-COMMAND returns True it will execute the STATEMENTS3.

You can have one or more elif clauses in the statement. The else clause is optional.

The conditions will be test sequentially and once any condition returns True the remaining conditions are not performed. The program control moves to the end of the if statements.

Let’s add an elif clause to the previous script:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The entered number is greater than 100."
elif [[ $VAR -eq 10 ]]
then
  echo "The entered number is equal to 100."
else
  echo "The entered number is less than 100."
fi

Nested if Statements

In Bash, you also can nest the if statements within if statements. You can place multiple if statement inside another if statement.

The following script will prompt you to enter three numbers and will print the largest number among the three numbers.

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]]
then
  if [[ $VAR1 -ge $VAR3 ]]
  then
    echo "$VAR1 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
else
  if [[ $VAR2 -ge $VAR3 ]]
  then
    echo "$VAR2 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
fi

It will show the output as following:

Enter the first number: 105
Enter the second number: 110
Enter the third number: 98
110 is the largest number.

Multiple Conditions

You can also use the logical OR and AND operators to check multiple conditions in the if statements.

For example, here is given one script to check the largest number among the three numbers. In this code, we removed the nested if statements, and we’re using the logical AND (&&) operator.

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]] && [[ $VAR1 -ge $VAR3 ]]
then
  echo "$VAR1 is the largest number."
elif [[ $VAR2 -ge $VAR1 ]] && [[ $VAR2 -ge $VAR3 ]]
then
  echo "$VAR2 is the largest number."
else
  echo "$VAR3 is the largest number."
fi

Test Operators

In Bash, following is syntax form of the test command:

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

Below are some of the most commonly used operators:

Conclusion

In this tutorial, you learned how to use the if, if..else and if..elif..else statements in Bash script.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version