How To Understand Bash If..Else Statement
This tutorial will walk you through the basics of the Bash if Statement and show you how to use it in your shell scripts.
Decision-making is one of the most fundamental concepts of computer programming. Like in any other programming language, if, if..else, if..elif..else, and nested if statements in Bash are used to execute code based on a certain condition.
if conditionals can have different forms. The most basic
if statement takes the following form:
The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.
If the TEST-COMMAND evaluates to True, the STATEMENTS gets executed. If TEST-COMMAND returns False, nothing happens; the STATEMENTS get ignored.
In general, it is a good practice always to indent your code and separate code blocks with blank lines. Most people choose to use either 4-space or 2-space indentation. Indentations and blank lines make your code more readable and organized.
Let’s look at the following example script that checks whether a given number is greater than 10:
Save the code in a file and run it from the command line:
The script will prompt you to enter a number. If, for example, you enter 15, the test command will evaluate to true because 15 is greater than 10, and the echo command inside the then clause will be executed.
The Bash if..else statement takes the following form:
If the TEST-COMMAND evaluates to True, the STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, the STATEMENTS2 will be executed. You can have only one else clause in the statement.
Let’s add an else clause to the previous example script:
If you run the code and enter a number, the script will print a different message based on whether the number is greater or less/equal to 10.
The Bash if..elif..else statement takes the following form:
If the TEST-COMMAND1 evaluates to True, the STATEMENTS1 will be executed. If the TEST-COMMAND2 evaluates to True, the STATEMENTS2 will be executed. If none of the test commands evaluate to True, the STATEMENTS2 is executed.
You can have one or more elif clauses in the statement. The else clause is optional.
The conditions are evaluated sequentially. Once a condition returns True, the remaining conditions are not performed, and program control moves to the end of the if statements.
Let’s add an elif clause to the previous script:
Nested if Statements
Bash allows you to nest if statements within if statements. You can place multiple if statements inside another if statement.
The following script will prompt you to enter three numbers and print the largest number among the three numbers.
Here is how the output will look like:
Generally, it is more efficient to use the case statement instead of nested if statements.
The logical OR and AND operators allow you to use multiple conditions in the if statements.
Here is another version of the script to print the largest number among the three numbers. In this version, instead of the nested if statements, we’re using the logical AND (&&) operator.
In Bash, the test command takes one of the following syntax forms:
To make the script portable, prefer using the old test [ command, which is available on all POSIX shells. The new upgraded version of the test command [[ (double brackets) is supported on most modern systems using Bash, Zsh, and Ksh as a default shell.
To negate the test expression, use the logical NOT (!) operator. When comparing strings , always use single or double quotes to avoid word splitting and globbing issues.
Below are some of the most commonly used operators:
-n VAR – True if the length of VAR is greater than zero.
-z VAR – True if the VAR is empty.
STRING1 = STRING2 – True if STRING1 and STRING2 are equal.
STRING1 != STRING2 – True if STRING1 and STRING2 are not equal.
INTEGER1 -eq INTEGER2 – True if INTEGER1 and INTEGER2 are equal.
INTEGER1 -gt INTEGER2 – True if INTEGER1 is greater than INTEGER2.
INTEGER1 -lt INTEGER2 – True if INTEGER1 is less than INTEGER2.
INTEGER1 -ge INTEGER2 – True if INTEGER1 is equal or greater than INTEGER2.
INTEGER1 -le INTEGER2 – True if INTEGER1 is equal or less than INTEGER2.
-h FILE – True if the FILE exists and is a symbolic link.
-r FILE – True if the FILE exists and is readable.
-w FILE – True if the FILE exists and is writable.
-x FILE – True if the FILE exists and is executable.
-d FILE – True if the FILE exists and is a directory.
-e FILE – True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
-f FILE – True if the FILE exists and is a regular file (not a directory or device).
The if, if..else and if..elif..else statements allow you to control the flow of the Bash script’s execution by evaluating given conditions.