Learning Center
Linux

How to Use if-else in Bash

18 Feb 2025
Hostman Team
Hostman Team

Many programming languages have conditional statements, such as if-else. These statements are also present in Bash, the default shell used in almost all modern Linux distributions. The if-else statements are used to check conditions — they allow the execution of specific commands depending on whether the condition is true or false. The if-else statements work exactly the same way as in any programming language.

In this article, we will discuss how to use if-else statements in the Bash shell through practical examples on a Linux VPS server.

The if Statement in Bash
Copy link

The if statement in Bash allows you to execute specific commands depending on the truth value of the given condition. Two logical statements are used to check for truth: True and False. The if statement is used when you need to check a condition. It controls the flow of script execution, allowing decisions to be made based on variable values, command results, and other conditions.

The if statement works as follows:

  1. First, the program checks the condition (the condition can be a command or a mathematical expression) contained in the if statement.
  2. If the condition is true, the program executes the commands listed after the then keyword.
  3. If the condition is false, the program executes the commands listed after the else statement.

The syntax of the if statement in Bash is as follows:

if [condition]; then
  # commands to execute if the condition is true
fi

Let's break down the operation of the if statement with a simple practical example. We will create a script that asks the user for a number, and if the number entered is greater than 10, the system will return the message "The number is greater than 10."

  1. Create a new file with a .sh extension, for example, using the nano editor:

nano greater_than_10.sh
  1. Insert the following code:

#!/bin/bash

read -p "Enter a number: " number

if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
fi
  1. Provide the file with execute permissions:

chmod +x greater_than_10.sh
  1. Now, run the script:

./greater_than_10.sh

Output:

Enter a number:

Enter any number, for example, 32, and press Enter. Since 32 is greater than 10, and this condition returns True, the program will execute the echo command.

Enter a number: 32
The number is greater than 10

Let’s break down the script in more detail:

  • The conditions are written in square brackets. In this example, the -gt operator is used (greater than, equivalent to the > symbol).
  • Next, we check the condition. If it’s True, the program executes the command after the then keyword.
  • The script ends with the fi keyword, signaling the end of the if block.

However, this script has one major drawback: it does not handle the case when the entered number is less than 10. The script will not return anything because there is no condition for that case.

To address this issue, we will use the else statement, which we will discuss in the next chapter.

The if-else Statement in Bash
Copy link

In the previous section, we ran a script with only one condition in the if statement — True. We didn’t specify any action for the False condition. As a result, if we entered a value leading to False, there was no response. If we want the script to perform specific actions for the false condition False, we need to use the else statement, which follows the if statement.

The if-else statement in Bash is used to perform conditional operations. It allows the execution of specific commands depending on whether the condition is true or false. The syntax for if-else is as follows:

if [condition]; then
    # commands executed if the condition is true
else
    # commands executed if the condition is false
fi

Remember that keywords, including if and else, in Bash shell scripts are case-sensitive. Be careful when using keywords in script files.

Let's consider using the if-else statements in a practical example. In this case, we will create a Bash script that asks the user for a number, and the system will display whether the number is greater than or less than 10.

  1. Create a new file with a .sh extension:

nano check.sh
  1. Insert the following code:

#!/bin/bash

read -p "Enter a number: " number

if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
else
    echo "The number is less than or equal to 10."
fi
  1. Grant the file execute permissions:

chmod +x check.sh
  1. Now, run the script:

./check.sh

The algorithm for the script works as follows:

  • After the if keyword, we specify the condition in square brackets. In this example, we use the -gt operator (greater than, equivalent to the > symbol).
  • The condition is checked. If the condition is true, the program executes the command after the then keyword— in this case, it prints the message "The number is greater than 10". If the condition is false, the program executes the command after the else keyword, printing the message "The number is less than or equal to 10".
  • Once one of the conditions is met, the program will end, as indicated by the fi keyword at the end.

Output if the number is greater than 10:

Enter a number: 56
The number is greater than 10.

Output if the number is less than 10:

Enter a number: 6
The number is less than or equal to 10.

Practical Use of if-else in Bash
Copy link

Let's look at the practical application of the if-else statement in Bash, which can be used when writing scripts.

Script Example 1. Checking if Run as root
Copy link

First, we will create a script that checks whether the script file is run as the root user. This can be useful when writing scripts that require root privileges, such as installing packages as the root user.

  1. Create a file named check-for-root.sh:

nano check-for-root.sh
  1. Use the following code to check for root user:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
  /usr/bin/printf "${R}>>>>${NC} Please run as root\n"
  exit 1
fi
  1. Grant the file execute permissions:

chmod +x check-for-root.sh
  1. And run it:
./check-for-root.sh

Image5

If the script is run as a regular user, the console will print the message "Please run as root".

The check for the root user uses the condition $EUID -ne 0, where:

  • $EUID is an environment variable that holds the numeric user ID. In Linux systems, the root user always has the ID 0, while other user accounts start at 1000.
  • -ne is a comparison operator meaning "not equal". Instead of ne, you can also use !=.

Script Example 2. Checking the Linux distribution
Copy link

Next, let's create another script that checks which Linux distribution is being used. If the script is run on Ubuntu, it will print the message "This is Ubuntu". If the script is run on any other Linux distribution, it will print "Not Ubuntu. You can run this script only on Ubuntu distributions".

  1. Create a file named check-for-distribution.sh:

nano check-for-distribution.sh
  1. Use the following code:

#!/bin/bash

dist=`grep DISTRIB_ID /etc/*-release | awk -F '=' '{print $2}'`

if [ "$dist" == "Ubuntu" ]; then
  echo "This is Ubuntu"
else
  echo "Not Ubuntu. You can run this script only on Ubuntu distributions"
fi
  1. Make the file executable:

chmod +x check-for-distribution.sh
  1. And run it:
./check-for-distribution.sh

Image6

If the script is run on an Ubuntu system, it will print "This is Ubuntu". On any other distribution, it will print "Not Ubuntu. You can run this script only on Ubuntu distributions".

Image10

Script Example 3. Checking if File Exists
Copy link

Now, let’s look at another practical example. We will create a Bash script that checks if a file named file1.txt exists. If it doesn't exist, the script will create it. The script checks for the file in the same directory it is run. If the file already exists, the script will print a message without creating the file.

  1. Create a file named check-file.sh:

nano check-file.sh
  1. Use the following script code:

#!/bin/bash

FILE="file1.txt"

if [ ! -f "$FILE" ]; then
    touch "$FILE"
    echo "$FILE has been created."
else
    echo "$FILE already exists."
fi
  1. Grant execute permissions for the script:

chmod +x check-file.sh
  1. Run the script:

./check-file.sh

Image12

If the file1.txt file already exists in the directory from which the script is run, you will see the message "file1.txt already exists.". The file will not be created.

Image4 (1)

Conclusion
Copy link

In this article, we reviewed the principles of logical statements such as if-else in the Bash shell and provided practical examples of using these statements. These examples are useful when writing scripts to automate system tasks or checks.