The If Conditional Statement and Logical Expressions in Bash
Shell scripting is useful when it comes to automating redundant tasks. I have written an article that will help you get started with shell scripting in bash, you can refer to it here.
Decision making is an important process in life. When writing bash scripts it is inevitable that you will need to make decisions on whether or not some instructions should be executed. To achieve this, conditionals and logical expressions and are used.
Conditional Statements
Conditionals are used to make decisions in a bash script. There are two major types of conditional statements in bash; if and case statements.
There are several variants of the if statement. A stand-alone if statement, an if-then-else statement, and an elif statement which is multiple if-then-else statements.
Case statements are similar to the switch statements that are found in popular programming languages such as C, Java, and Python just to name a few.
Basic If Statement
In this tutorial, I will cover the if statement. The if statement evaluates a logical expression. If the logical expression is true, the code inside the if-statement is executed. The syntax for this is shown below:
The then keyword is required even if only one statement is to be executed. The keyword fi signifies the end of the if statement, it is also required and without it, you will get a syntax error.
This code can be written in fewer lines by having then in the same line with the if statement. If and then are keywords in bash and in bash keywords shouldn’t be in the same line. To write them in the same line, a semicolon is used to terminate the lines. The syntax for this is shown below:
I will use this syntax for the rest of the tutorial as it is more readable.
If-then-else Statement
For a scenario where some statement(s) should be executed if the logical expression evaluates to true and others when it evaluates to false, an if-then-else statement is used. If the logical expression evaluates to true, the code inside the if-statement is executed if it evaluates to false the code inside the else section is executed. The syntax for this is:
Elif Statement
If the decision to be made is a bit complex, multiple if statements will be needed. To achieve this, the elif statement is used. Its syntax is shown below:
In this case, there are multiple decisions to be made. The first logical expression that evaluates to true runs.
Important Tidbits
- Use square brackets as opposed to parentheses as this does not work in all versions of Linux.
- There should be whitespace around the content of the square brackets. Failure to do this will result in syntax errors.
- If you are using string variables in the logical expressions, always quote them as they may contain spaces which will lead to them being treated as separate arguments.
- Don’t forget the semicolon after the if statement if you’re using the syntax that has then in the same line with if.
Logical Expressions
Logical expressions/operators are the ‘decision’ part of a conditional statement. They evaluate to either true or false and dictate the part of the conditional that will run.
Logical expressions can be used to perform string and integer comparisons as well as to check if a string is a file name or a directory.
The tables show some common logical expressions and their meaning.
String Comparison
Integer Comparison
Files and Directories
Combining Conditions
Let’s Practice
Let’s take what we have learnt and write a simple script that uses elif to perform integer comparison. The script will take in an argument and print out whether it’s less than 0, equal to 0 or greater than 0.
You can use an editor of your choice be it vim/vi, nano, gedit etc to copy this code and run it. You can refer to my previous article if you need help in this.