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.
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:
if
statement.then
keyword.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."
Create a new file with a .sh
extension, for example, using the nano
editor:
nano greater_than_10.sh
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
Provide the file with execute permissions:
chmod +x greater_than_10.sh
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:
-gt
operator is used (greater than, equivalent to the >
symbol).then
keyword.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.
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.
Create a new file with a .sh
extension:
nano check.sh
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
Grant the file execute permissions:
chmod +x check.sh
Now, run the script:
./check.sh
The algorithm for the script works as follows:
if
keyword, we specify the condition in square brackets. In this example, we use the -gt
operator (greater than, equivalent to the >
symbol).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".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.
Let's look at the practical application of the if-else
statement in Bash, which can be used when writing scripts.
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.
Create a file named check-for-root.sh
:
nano check-for-root.sh
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
Grant the file execute permissions:
chmod +x check-for-root.sh
./check-for-root.sh
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 !=
.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".
Create a file named check-for-distribution.sh
:
nano check-for-distribution.sh
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
Make the file executable:
chmod +x check-for-distribution.sh
./check-for-distribution.sh
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".
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.
Create a file named check-file.sh
:
nano check-file.sh
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
Grant execute permissions for the script:
chmod +x check-file.sh
Run the script:
./check-file.sh
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.
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.