How to Use Bash If Statements on Linux

Are you looking for a tutorial on how to use Bash If Statements on Linux? Then this guide will be helpful for you.

Conditionals are a major part of any programming language. When writing programs, we need to do a lot of decision-making based on a particular scenario.

Think about the English language for a second. We say a lot of sentences that go like “If this then that”. Conditional statements help us determine the control flow in a program.

In this tutorial, we will have an in-depth look at If statements in Bash. Let’s get started!

How to Use Bash If Statements on Linux

For easier understanding, we will divide the whole guide into smaller parts and cover the essential knowledge in each one.

Basic Syntax of If Statements in Bash

Let’s start with the very basics—the syntax of if statements in Bash. If you’re familiar with some other programming language syntax, you may quickly start writing Bash scripts similarly. Don’t make that mistake. While there might be some slight similarity between the syntax, Bash if statements are quite different.

At the very minimum, this is what if statements look like in Bash:

$ if [ condition ]
then

    <command to be executed>

fi

Let’s break down what’s happening here. We start with the keyword ‘if’. Then we write two square brackets. These square brackets are known as the test command in Bash. Inside it, we write our condition. A condition is a boolean expression. We check whether the condition is true or false.

Notice the spaces inside the square brackets before and after the condition. This is essential to separate the arguments from the command. If you omit the spaces, you may face syntax errors.

Next, we add the ‘then’ keyword. In the next line, we write the command we want to execute if the condition is true. We use a tab character for indentation to make the code readable. Lastly, we finish the ‘if’ code segment with the keyword ‘fi’.

How the If Statements Work in Bash

So now you know the basic structure of Bash if statements. Now let’s learn how they work. For this, we will create a simple bash script. We will use nano for that. Open your preferred text editor so that you can follow along. Give a name to the file. We named it age.sh. Write down the following bash script:

$ #!/bin/bash
    
age=25

if [ "$age" -ge 18 ]

then

    echo "You are an adult."

fi
How to Use Bash If Statements on Linux

Save the file with ‘Ctrl+O’ and then exit with ‘Ctrl+X’. Now make the file into an executable by running this command:

$ chmod u+x age.sh

Output:

make file executable

Now run the script with this command:

$ ./age.sh

Output:

run bash script

Let’s understand the control flow. The script checks for the condition in the first line. If the condition is true, then the command after the then statement is executed. If the condition is false, then the code is not executed.

In the above example, we set the variable ‘age’ value to 25. In the condition, we are comparing the age variable with 18. If it’s greater than or equal to 18, then the command below is executed, as in this script. Also, notice the spaces used before and after ‘-ge’. These are necessary to use so that the script works.

How to Use the if… else Statements

So you now may think that currently, nothing happens if the if statement condition is false. What if we want to do something if that’s the case? Then we add the ‘else’ statement with the if. Let’s see another example:

#!/bin/bash

hour=10

if [ "$hour" -lt 12 ]

then

    echo "Good morning!"

else

    echo "Good afternoon!"

fi
How to Use Bash If Statements on Linux

In this example, we check whether the current hour is less than 12 or not. If it is, then we print “Good morning!” in the terminal. If it’s not, then we print “Good afternoon!”. In this case, the condition is true and will execute the code in the ‘then’ statement.

run bash script

But now let’s make the condition false on purpose. To do that, change the value of the ‘hour’ variable to something greater than 12.

How to Use Bash If Statements on Linux

Now if we run this script, we should get the alternative output, which is “Good afternoon!”.

run bash script

In short, if the condition inside the square brackets is true, the ‘then’ statement is executed. If it’s false, then the ‘else’ statement is executed.

How to Use if…elif…else Statements

There’s another part of if statements you should know about. In the previous examples, we only used one condition. But there could be multiple conditions, right? 

In such cases, we use the ‘elif’ statement. The first condition goes inside the if statement, the other ones go inside the ‘elif’ statements. You can use as many elif statements as you want. Let’s see an example to see how it works:

#!/bin/bash

day=$(date +%u)

if [ "$day" -eq 1 ]

then

    echo "It's Monday!"

elif [ "$day" -eq 2 ]

then

    echo "It's Tuesday!"

elif [ "$day" -eq 3 ]

then

    echo "It's Wednesday!"

elif [ "$day" -eq 4 ]

then

    echo "It's Thursday!"

elif [ "$day" -eq 5 ]

then

    echo "It's Friday!"

elif [ "$day" -eq 6 ]

then

    echo "It's Saturday!"

elif [ "$day" -eq 7 ]

then

    echo "It's Sunday!"

else

    echo "Invalid day of the week."

fi
How to Use Bash If Statements on Linux

In this script, the code uses the date command with the %u format specifier to retrieve the current day of the week as a number. It starts from 1 and goes till 7 where 1 represents Monday, 2 represents Tuesday, and so on. Since it’s Thursday when we’re writing this, that should be the script’s output. Run the script and see the output.

run bash script

It shows Thursday, as expected. The script checks for each condition one at a time and then executes the one that evaluates to true. In this case, the 4th condition was true, and so we got that output.

Final Thoughts

This tutorial shows you how to use Bash if statements on Linux. We’ve covered the basic syntax of the if statements and the different ways you can use them. We also highlighted some important matters in the syntax which a beginner may miss. Now it’s your turn to try and test this new knowledge and take your Bash scripting to another level.

If this guide helped you, please share it.

Related Posts