🐧 Bash if statements: if, elif, else, then, fi

If you are just starting out with the scripting language Bash, you will soon find that you need to use conditional statements.

In other words, conditional statements define “if the condition is true or false, then do this or that, and if the opposite is true, do something else.”

This is the most basic function of any conditional statement.

This article will introduce you to the five basic if statements, namely if, elif, else, then, and fi.

The first simply opens an if statement, then enters the section which commands to execute if the statement was true, and the else enters which commands to execute if the statement was false.

Finally, fi closes the statement.

We also have a special elif which we will learn more about later.

Let’s start with a simple example.

In this guide, you will learn:

  • How to implement an if statement on the Bash command line
  • How such if statements can also be used inside Bash scripts
  • Examples showing if, elif, else, then, and fi in Bash

Example 1: a simple if statement on the command line

$ if [ 1 -eq 1 ]; then echo "Matched!"; fi
Matched!

In this example, we are comparing one to one.

Note that -eq means equal.

To do the opposite, you can use -ne, which means not equal, as shown in the following example:

$ if [ 0 -ne 1 ]; then echo "Matched!"; fi
Matched!

In this case, we checked for inequality, and since 0 is not equal to 1, the if statement is correct and the commands after then will be executed. Let’s change this a bit:

$ if [ 1 -ne 1 ]; then echo "Matched!"; else echo "Not Matched!"; fi
Not Matched!

Here we have introduced the else statement; what commands to execute if the condition in the if statement turns out to be false (or incorrect).

When we try to ask if 1 (-ne) is 1 this time, and since 1 is indeed 1, the condition formulated in this if statement is false, and we encounter our else statement with the matching text printed out.

Example 2: Using an if statement in a Bash shell script

It’s worth noting that you can easily copy and paste any if statement shown here or elsewhere and use it inside a Bash shell script.

For example:

$ echo '#!/bin/bash' > myscript.sh
$ echo 'if [ 1 -eq 1 ]; then echo "Matched!"; fi' >> myscript.sh 
$ chmod +x myscript.sh 
$ ./myscript.sh 
Matched!
$ 

Here we just created a small shell script myscript.sh using echo and the redirector> to redirect the output from our echo to a file.

When you use> a new file will be created and any file with the same name will be overwritten, so use with caution.

Then we add our if statement again using echo and the double redirector >> which, unlike>, does not create a new file, but simply adds text to the specified file.

We then chmod + x to the script to make it executable, and execute the script with the ./ prefix, which bash requires (any valid pathname will do).

The first line of the script just makes sure that we will be using the bash interpreter for our script.

It is recommended to always install it in bash and other scripts (for other scripts, you can install any interpreter that will execute your script, for example #! / Usr / bin / python3 for Python 3 (.py3 for example), etc.).

When we execute the script, we see that the result is generated as expected (1 equals 1).

Matched!

Example 3: What is elif?

The elif operator provides additional abbreviation flexibility by minimizing the need for nested operators.

Consider the following script test.sh:

#!/bin/bash
if [ 0 -eq 1 ]; then 
  echo '0=1'
else
  if [ 0 -eq 2 ]; then
    echo '0=2'
  else
    echo '0!=2'
  fi
fi

And the output from it:

$ ./test.sh
0!=2

Here we have gone through the first if statement, and since 0 does not match 1, the else is activated. This happens the second time, when 0 is also not equal to 2, and therefore the -eq (equal to) condition is not met, and the second else is activated, yielding 0! = 2. Let’s compare this to the elif based operator in the following test2.sh.

#!/bin/bash
if [ 0 -eq 1 ]; then
  echo '0=1'
elif [ 0 -eq 2 ]; then
  echo '0=2'
else
  echo '0!=2'
fi

Conclusion:

$ ./test2.sh
0!=2

The script did the same, but in a much more flexible and shorter way, requiring only one level of if statement depth and with cleaner general code.

Also note that it is possible to have a single if statement followed by many elseif statements, which allows the developer to test multiple conditions and is a neat single-level structure.

Conclusion

In this article, we looked at examples illustrating the if, elif, else, then, and fi statements in Bash.

We also looked at how to implement if statements on the Bash command line.

We also looked at porting such statements to Bash scripts.

Enjoy Bash if statements and share your best tips and tricks with us!

Sidebar