Conditional statements allow us to perform different actions based on different conditions. The
most common conditional Bash statements include if, else, and elif.
The if statement is relatively simple–it checks to see if a condition is true–but it requires a very
specific syntax. Pay careful attention to this syntax, especially the use of required spaces:
1 | if [ <some test> ] |
In this listing, if “some test” evaluates as true, the script will “perform an action”, or any commands
between then and fi. Let’s look at an actual example:
1 | kali@kali:~$ cat ./if.sh |
In this example, we used an if statement to check the age entered by a user. If the entered age was
less than (-lt) 16, the script would output a warning message.
The square brackets (“[“ and “]”) in the if statement above are actually a reference to the test
command. This simply means we can use all of the operators that are allowed by the test
command. Some of the most common operators include:
| Operator | Description: Expression True if… |
|---|---|
| !EXPRESSION | The EXPRESSION is false. |
| -n STRING | STRING length is greater than zero |
| -z STRING | The length of STRING is zero (empty) |
| STRING1 != STRING2 | STRING1 is not equal to STRING2 |
| STRING1 = STRING2 | STRING1 is equal to STRING2 |
| INTEGER1 -eq INTEGER2 | INTEGER1 is equal to INTEGER2 |
| INTEGER1 -ne INTEGER2 | INTEGER1 is not equal to INTEGER2 |
| INTEGER1 -gt INTEGER2 | INTEGER1 is greater than INTEGER2 |
| INTEGER1 -lt INTEGER2 | INTEGER1 is less than INTEGER2 |
| INTEGER1 -ge INTEGER2 | INTEGER1 is greater than or equal to INTEGER 2 |
| INTEGER1 -le INTEGER2 | INTEGER1 is less than or equal to INTEGER 2 |
| -d FILE | FILE exists and is a directory |
| -e FILE | FILE exists |
| -r FILE | FILE exists and has read permission |
| -s FILE | FILE exists and it is not empty |
| -w FILE | FILE exists and has write permission |
| -x FILE | FILE exists and has execute permission |
With the above in mind, our previous example using if can be rewritten without square brackets as
follows:
1 | kali@kali:~$ cat ./if2.sh |
Even though this example is functionally equivalent to the example using square brackets, using
square brackets makes the code slightly easier to read.
We can also perform a certain set of actions if a statement is true and another set if it is false. To
do this, we can use the else statement, which has the following syntax:
1 | if [ <some test> ] |
Let’s extend our previous “age” example to include the else statement:
1 | kali@kali:~$ cat ./else.sh |
Notice that the else statement was executed when the entered age was greater than (or more
specifically “not less than”) sixteen.
The if and else statements only allow two code execution branches. We can add additional
branches with the elif statement which uses the following pattern:
1 | if [ <some test> ] |
Let’s again extend our “age” example to include the elif statement:
1 | kali@kali:~$ cat ./elif.sh |
In this example, the code execution flow was slightly more complex. In order of operation, the then
branch executes if the entered age is less than sixteen, the elif branch is entered (and the “Hats
off..” message displayed) if the age is greater than sixty, and the else branch executes only if the
age is greater than sixteen but less than sixty.