BASH Programming

How to use the Bash “if” Statement?

Whenever you are writing a program, conditions play a very important role in defining the overall workflow of your program. These conditions are usually specified with an “if” statement, which is as commonly used in any other programming language as in Bash. Today’s discussion aims to explain to you the method of using the “if” statement in Bash.

Note: For explaining the usage of the Bash “if” statement, we have worked with Linux Mint 20.

Method of Using the Bash “if” Statement

The logic behind using the conditional “if” statement is the same in Bash as it is in any other programming language. However, its syntax slightly differs. To familiarize the reader with that syntax, we will share with you some examples that will depict the usage of the Bash “if” statement in Linux Mint 20 below.

Example # 1: Simple “if” Statement

This example is there just to give you the basic overview of the “if” statement through which you will be able to learn its syntax very well. For proceeding with this example, we will perform the steps shown below:

First, we will create a Bash file in our Home directory so that it is easily accessible. We will simply create an empty document and give it a name of our choice with a “.sh” extension. It is always good to give meaningful names to your files so that it is easier for you to locate them whenever you look back. Since in this article, we aim to learn the usage of the “if” statement in Bash, that is why we have named our file as Bash_if.sh as shown in the following image:

After creating this file, you have to open it so that you can edit it. Now, you are supposed to type the script shown below in your newly created file. Here, the line “#!/bin/bash” is there to represent that the proceeding script is a Bash script. Then we have simply shown a message on the terminal. We have also created a variable “name” in which we are going to store the input provided by the user through the “read” command. Then comes the “if” statement in which we are comparing the value of the “name” variable with a string. If a match is found, then the “then” part of the statement will be executed, and the specified message will be displayed on the terminal. Finally, we have closed our “if” statement with a “fi” statement, which is mandatory.

Once you have programmed your script, press Ctrl+ S to save your file. Then, it is time to run this script via the terminal with the following command:

$ bash Bash_if.sh

After executing this script, you will be asked to enter any name of your choice, as we have already stated in our script. For checking if our “if” statement works correctly or not, we have provided the name “Aqsa” as shown in the image below:

When you enter this name, the “then” part of our script will be executed, and a message will be printed on the screen, which will be an indication that our “if” statement is working exactly as it was intended to. As shown in the following image:

Example # 2: “if-else” Statement

In this example, we are going to slightly modify the scenario discussed above in the following manner:

Just imagine for a while that what if a user enters any name other than “Aqsa”? Obviously, the script that we have created above will fail because it is not designed to cater to any other case. This is why in the script shown in the image below, we have added an “else” part to our previous script, that will serve all the other cases in which a user enters any name other than the one specified in the “if” statement. This script will ensure that no matter what name the user enters, your script will still function correctly.

When you will run this modified script with the “bash” command, you will be asked to enter your name again. This time, we have intentionally entered a different name to check whether our modified script works correctly or not, as shown in the following image:

In the output of this script, you will notice that the “else” part of our script has been executed, which will indicate that our modified script also works perfectly well.

Example # 3: Multiple Conditions with “if” Statement

The “if” statement in Bash also allows you to apply multiple conditions at once that are separated by the “AND” or “OR” operator; depending on the scenario. This can be explained by the following steps:

You are required to type the script shown in the image below in your Bash file. In this script, we have asked the user to enter any number from 1 to 10. After getting the user input, this script checks whether the number that you entered is greater than or equal to “1” and less than or equal to “10”. These two conditions are separated by the “AND- &&” operator. If both these conditions are met, then a message saying that you are within the range will be printed on the terminal. Otherwise, the control will jump to the “elif” part of the script, which has another two conditions. These conditions are there to check if the entered number is less than “1” or greater than “10”. These two conditions are separated by the “OR- ||” operator. If either of these conditions is met, then a message will be displayed on the terminal that says you are going beyond the range.

To test this script, we will first enter a number between 1 and 10. We have entered “5”, which is the average case. Hence, our script has printed the message from the “if” block.

Now, we wanted to see if the “elif” part is executed or not. Doing so, we have deliberately entered “0” which is less than “1”, and also beyond the provided range. Through the output of the script, you can easily verify that the “elif” part has been executed.

Conclusion

By going through this tutorial, you should be in a good position to write a reasonable Bash script having an “if” or multiple “if” statements with different conditions. This will not only polish your programming skills but will also make those tasks that seem impossible to accomplish easier without the conditional statements.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.