Skip to content

Bash ‘if else’ statement examples

In a programming language, conditional statements allow us to make decisions based on a certain outcome. If you’ve studied the basics of just about any language, you know just how handy the ‘if else’ statements can be. 

Bash also has its own variations of the ‘if else’ statements as listed below.

In this article, we will focus on the ‘if else’ statement and work with some examples to see how the whole thing works. 

Also read: KDE vs Gnome: Pros and Cons


If statement in Bash

The general syntax for an if statement in Bash is as follows. 

if test-condition
then
  Task 1
fi

Here, if the test-condition is true, Task 1 will be executed. Otherwise, the code will move on to the next line. 

Here’s an example to check whether a given number is greater than ten. Notice how the if statement is used to evaluate the condition. 

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The variable is greater than 100."
fi

When you run this code, any number greater than 100 will trigger the if statement and cause the output to be the specified echo line. Otherwise, the program would output nothing. 

Also read: Top 7 coding games


If-else statement in Bash

The general syntax for if-else statements in Bash is as follows. 

if main-command
then
  Task 1
else
  task 2
fi

Here if the main-command condition is true, then Task 1 will be executed. Otherwise, Task 2 will be executed. 

Let’s take an example where we have to check if a number is greater than ten or less/equal to then and print a different output based on the number. 

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The variable is greater than 100."
else
  echo "The variable is equal or less than 100."
fi

In the above code, if you enter any value greater than 100, the first echo statement preceding then will be printed. Otherwise, if the number is less than or equal to 100, the second echo command will be printed. 


If-elif-else statement in Bash

The general syntax for if-elif-else statements in Bash is as follows. 

if test-condition
then
  Task 1
elif test-condition-2
then
  Task 2
else
  Task 3
fi

In the above snippet, if the test-condition is true, Task 1 will be executed. Otherwise, if test-condition-2 is true, Task 2 will be executed. If none of the given conditions is true, Task 3 will be executed. 

Note that you can have more than one elif statements in your program. Use of the else statement is optional. Let’s add this elif statement to the previous example. 

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The variable is greater than 100."
elif [[ $VAR -eq 100 ]]
then
  echo "The variable is equal to 100."
else
  echo "The variable is less than 100."
fi

This script will first check if a number is greater than 100. If yes, the first echo statement will be printed. If not, the script will move on and check if the number is equal to 100. If the condition is true, the second echo statement will be printed. 

Lastly, if none of the conditions was true, the last echo statement will be printed. 

Also read: How to fix Gitignore not working issue?


Nested if statements in Bash

Nested if statements are basically if statements inside of another if statement. This allows programmers to check for a condition inside a condition. Here’s the general syntax.

if test-condition-1
then
    if test-condition-2
    then
        echo "There are two nested ifs in this snippet"
fi

As you can see, if the first condition is true, the program will branch in and check the other condition. Only when both the conditions are true will the echo statement will be printed. 


Mutliple conditions using if statements

Using logical operators such as OR or AND allows programmers to use multiple conditions in if statements. This can help you check multiple conditions at the same time and avoid nested if statements.

if test-condition-1 && test-condition-2
then
    echo "This echo will only print when both the above conditions are true."
fi

if test-condition-3 || test-condition-4
then
    echo "This echo will print when either of the above conditions is true."
fi

Also read: Top 11 Ubuntu themes to customise your installation

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.







>