DEV Community

Gilbert Nkolo Acquostic
Gilbert Nkolo Acquostic

Posted on

How to use the If...else conditional statement in Bash

In this article, I will walk you through the basics of the bash if...else statement and how to use it in your shell scripts to automate system processes.

Decision-making is one of the most fundamental concepts of computer programming. Like in any other programming language, if, if...else, if...elif...else, and nested if statements in Bash are used to execute code when a specific condition is met.

Bash if...else Statement

This article will walk you through the basics of the bash if...else statement and explain you how to use it in your shell scripts.

Decision-making is one of the most fundamental concepts of computer programming. Like in any other programming language, if, if...else, if...elif...else, and nested if statements in Bash are used to execute code when a specific condition is met.

The if Statement

Bash if conditionals can have different forms. The most basic if statement takes the following form:

if TEST-COMMAND
then
  STATEMENTS
fi
Copy
Enter fullscreen mode Exit fullscreen mode

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

If the TEST-COMMAND evaluates to True, the STATEMENTS are executed. If the TEST-COMMAND returns False, nothing happens; the STATEMENTS are ignored.

Generally, it is always good practice to indent your code and separate code blocks with blank lines. Most people choose to use either 4-space or 2-space indentation. Indentations and blank lines make your code more readable and organized.

Let’s look at the following example script that checks whether a given number is greater than 10:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
fi
Enter fullscreen mode Exit fullscreen mode

Save the code in a file and run it from the command line:

bash test.sh
Enter fullscreen mode Exit fullscreen mode

Enter a number for example 12 to test the If statement.

The variable is greater than 10.
Enter fullscreen mode Exit fullscreen mode

The if...else Statement

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

if TEST-COMMAND
then
  STATEMENTS1
else
  STATEMENTS2
fi
Enter fullscreen mode Exit fullscreen mode

If the TEST-COMMAND evaluates to True, the STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, the STATEMENTS2 will be executed. You can have only one else clause in the statement.

Let’s add an else clause to the previous example script:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
else
  echo "The variable is equal or less than 10."
fi
Enter fullscreen mode Exit fullscreen mode

Now, let's run the script again.

bash test.sh
Enter fullscreen mode Exit fullscreen mode

This time around, enter a number less than 10 and notice the output changes to the else message.

The variable is equal or less than 10.
Enter fullscreen mode Exit fullscreen mode

Enter a number greater than 10.

The variable is greater than 10.
Enter fullscreen mode Exit fullscreen mode

If you run the code and enter a number, the script will print a different message based on whether the number is greater or less/equal to 10.

Top comments (0)