Home / Linux/Unix/BSD / How To Use Bash if else Statement

How To Use Bash if else Statement

Decision making is at the heart of computer programming. Which is why conditional statements play a crucial role in the programming language.

It allows you to test whether conditions in your program are true or not. To execute the program, you need to work with bash and shell scripting.

In this guide, I’ll teach you how to use the Bash if else statement and how you can use it in shell scripts.

Four Types Of if Statements

Bash conditional statements vary in form, depending on how you use them. There are four types of if statements:

  • bash if statement
  • bash if-else statement
  • bash else if (if-elif-else) statement
  • bash nested if statement

There are four different keywords you can use to execute a bash if else statement. Below is how you can use them:

  • if – to validate the condition
  • then – if the former condition is true, it will execute the specific command
  • else – if the former condition is true, it will execute an alternate command
  • fi – to close the “if, then, else” statement

Now you’re familiar with the uses of the different statements and keywords. Next, I’ll show you how you can apply these to real-world examples.

Bash if Statement

If statement is the simplest form, which looks like this:

if [ [ test-command ] ]
then
        <execute command1>
fi

First, you start with the “if” keyword before stating a specific command for your program.

If the test command evaluates to true, the “then” keyword executes the action you want to happen. But if it’s false, the program doesn’t run anything.

A rule of thumb is to indent your code and separate code blocks with a space. When using “if else” statements, make sure they’re within single or double squared brackets.

Below is an example of a program that checks whether the value you input is greater than 10:

#!/bin/bash
read -p "Enter a number: "
read VAR
if [ [ $VAR -gt 10 ] ]
then
  echo "The value is greater than 10."
fi

Save the code as a file to make sure that the file exists before running it. You can then use the following command line:

$ bash test.sh

Bash if-else Statement

The “else” clause allows you to add another command if the former statement is false. The if else bash statement looks like this:

if [ [ test-command ] ]
then
        <execute command1>
else
        <execute command2>
fi

You can only have one “else” clause for the statement. You may also opt to include the “else” clause or not. Once removed, nothing happens if the condition evaluates to false.

It’s important to add a space to the command from the squared-bracket. Otherwise, it leads to a syntax error and the program will not run.

Let’s take our first bash if statement as an example. Here, we add an “else” clause to run another command if the value inputted isn’t greater than 10.

#!/bin/bash
read -p "Enter a number: "
read VAR
if [ [ $VAR -gt 10 ] ]
then
  echo "The value is greater than 10."
else
        echo "The value is equal to or lesser than 10."
fi

If the test command evaluates to true, which is greater than 10, it will execute the first command.

If the value isn’t greater than 10, the condition evaluates to false. This causes the program to execute another command.

Bash if-elif-else Statement

The elif bash (else if) statement allows you to check multiple conditions by consequence. Below is how the bash elif looks like:

if [ [ test-command1 ] ]
then
        echo <execute command1>
elif [ [ test-command2 ] ]
then
        echo <execute command2>
else
        echo <execute command3>
fi

If the first test command evaluates to true, it executes the first action you want to happen. Next in line is the command with the “elif” clause.

Otherwise, if the elif command is true, it executes the second action in line. But if both elif conditions are false, it executes the command under “else”.

You can add more than one “elif” cause in the bash else if statement. Once a condition is true, it doesn’t perform the remaining commands.

Here’s how you can apply the bash else if statement to our earlier example:

#!/bin/bash
read -p "Enter a number: "
read VAR
if [ [ $VAR -gt 10 ] ]
then
  echo "The value is greater than 10."
elif [ [ $VAR -eq 10 ] ]
then
  echo "The value is equal to 10."
else
        echo "The value is lesser than 10."
fi

In this elif example, the program executes a different command for a false condition. If the value inputted is equal to 10, there is separate action for that specific scenario.

Since in bash elif, the “else” clause remains optional, you may opt to remove it.

Multiple Conditions In If-Elif-Else

Let’s say you want to apply else if bash to multiple conditions. You can do this by separating one condition from another using the “and” clause or the “or” clause.

Below is how you can use “and” in bash if-elif-else:

#!/bin/bash
if [ [ <test-command1> && <test-command2> ] ];
then
   <execute command>
fi
As for the "or" in elif, here is an example:
#!/bin/bash
if [ [ <test-command1> | | <test-command2> ] ];
then
   <execute command1>
else
        <execute command2>
fi

Bash Nested-if Statement

Among all statements mentioned, Nested if is the most complex. This allows you to nest if statements within if statements.

It may sound complicated, but it’s useful for a preliminary validation of a condition. Below is how the structure for nested if looks like:

if [ [ test-command ] ]
then
if [ [ test-command2 ] ]
then
        <execute command1>
else
        <execute command2>
        fi
fi

For example, you want to run a program that determines the greatest value among three choices. Here’s how you can use the condition:

#!/bin/bash
read -p "Enter value of a :" a
read -p "Enter value of b :" b
read -p "Enter value of c :" c
if [ [ $a -gt $b ] ]
then
      if [ [ $b -gt $c ] ]
      then
            echo "a is the greatest."
      else
            echo "c is the greatest."
      fi
else
      if [ [ $b -gt $c ] ]
      then
            echo "b is the greatest."
      else
            echo "c is the greatest."
      fi
fi

The output will show as follows:

Enter value of a: 3
Enter value of b: 10
Enter value of c: 6
10 is the greatest.

In bash, you can also use test operators such as integer, string, or file to validate conditions. This helps you check whether a file exists.

Conclusion

For a program to run, you need to define the condition and the resulting action.

The basic uses of bash script are using statements if, if else, elif, and nested if. These statements are vital for decision-making and for evaluating conditions.