Bash if Statements: if, elif, else, then, fi

If you are just starting to explore the Bash coding language, you will soon find yourself wanting to create conditional statements. Conditional statements, in other words, define ‘if a 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 statement clauses. being if, elif, else, then and fi. The first simply opens a if statement, the then introduces the what commands to execute if the statement condition was true section and the else introduces the what commands to execute if the statement condition was false section. Finally, the fi closes the statement. We also have the special elif on which we will see more in a minute. Let’s start with an easy example.

In this tutorial you will learn:

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

Bash if Statements: if, elif, else, then, fi

Bash if Statements: if, elif, else, then, fi

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires linux-commands to be executed as a regular non-privileged user

Example 1: Simple if statement at the command line

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

In this statement, we are comparing one to one. Note that -eq mean equal to. To do the reverse, one can use -ne which means not equal to, as shown in the following example:

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

In this case, we checked for non-equality, and as 0 is not equal to 1 the if statement is true, and the commands after the then will be executed. Let’s change this slightly:

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

Here we introduced an else clause; what commands to execute when the condition in the if statement has proven to be false (or not true). As we trying to query whether 1 is not equal (-ne) to 1 this time, and as 1 does equal 1 (wich is not the case), the condition formulated in this if statement is false, and we run into our else statement with the matching text printed.

Example 2: Using and if statement from within a Bash shell script

It is good to note 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 simply created a small myscript.sh shell script by 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 please use it with care. Next we add our if statement again by using echo and a double redirector >> which unlike > will not create a new file, and simply append text to the file indicated.

Next we chmod +x the script to make it executable, and execute the script using the ./ prefix which is required in Bash (any correct path specifier will do).

The first line of the script is simply making sure that we will be using the Bash interpreter for our script. It is good practice to always set this for Bash and other scripts (for other scripts, you will want to set this to whatever interpreter which is going to execute your script, for example #!/usr/bin/python3 for a Python 3 (.py3 for example) scripts etc).

When we execute the script we can see that the output is being generated as expected (1 matches 1): Matched!.

Example 3: What is elif?

The elif clause provides us with extra shorthand flexibility short-cutting the need nested statements. Consider the following 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 thereof:

$ ./test.sh
0!=2


Here we stepped through the first if statement, and since 0 does not match 1, the else clause is activated. This happens a second time when 0 also proves unequal to 2 and hence the -eq (equal to) condition fails, and the second else clause is activated, giving as output 0!=2. Let’s compare this with an elif based statement 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

And the output thereof:

$ ./test2.sh
0!=2

The script did exactly the same, but in a much more flexible and shorter way, requiring only one level of if statement depth and with cleaner overall code. Note also that it is possible to have one if statement followed by many elseif statements, allowing a developer to test a variety of conditions is a neat looking, single level structure.

Conclusion

In this article, we explored examples exemplifying the if, elif, else, then and fi clauses in Bash. We also looked at how to implement if statements at the Bash command line. We also looked at moving such statements into Bash scripts. Enjoy if statements in Bash, and leave us some thoughts with your best if tips and tricks!

And, for a somewhat more advanced look at what if can do for you when combined with subshells, checkout our How To Use Bash Subshells Inside If Statements article!



Comments and Discussions
Linux Forum