The if..else statements are categorized under conditional statements in
bash. They work similarly to the if..else statements in programming languages.
This tutorial will introduce the following variants of
bash if..else statements, along with explanations and examples.
- if statement
- if-else statement
- if-elif-else statement
You will also learn to use the nested if statements in the latter segment.
Table of Contents
Bash if Statement
The bash if statement evaluates whether the expression is true. It sets a condition where the expression is evaluated, and corresponding program statements are executed if it is true. The syntax of the if statement takes the following form.
if expression then statements fi
Here, the expression is evaluated. If it is true, the statements after the then statement will be executed. If the expression is false, the statements won’t run, and the conditional check will stop after it finds the fi command. The
fi command denotes the end of the last if statement.
Now, let’s understand the if statement with a demonstration. The bash script will display a message if the user is eligible to cast a vote.
read -p "Enter your age: " age if $((age)) -gt 17 then echo "you can vote" fi
The above script asks for age as input from the user. It will print a message that says you can vote if the entered age is greater than 17. The user input age is stored in the age variable. Inside the if statement, the
-gt operator checks whether the variable age is greater than 17. Here,
gt signifies greater than.
Let’s test the above script with different inputs. The following is the output when the age is 20.
Enter your age: 18 you can vote
The following is the output when the age is 17.
Enter your age: 17
Here, the condition does not match, and the script ends its execution without any message.
Bash if-else Statement
The if-else statement is an extension of the if statements where the else part is also included. Previously, in the if statement, the commands only got executed if the evaluated expression was true.
With the if-else statement, you can specify the commands to be executed even when the expression is false. The syntax of the if-else statement takes the following form.
if expression then statement1 else statement2 fi
The syntax is pretty easy to understand as you have already got the idea of the if statement. If the expression returns true, the
statement1 will execute after the then statement. If that is not the case or the expression evaluates to false, then the
statements2, which is after the else statement, will execute.
Now, let’s look at the following example to vividly understand the concept. The following bash script is the extension of the example used in the if statement section above.
The additional feature of this script is it will also display the message if the user is not eligible to cast a vote.
read -p "Enter your age: " age if $((age)) -gt 17 then echo "you can vote" else echo "you cant vote" fi
Let’s test the script above with different inputs and see the results below.
Enter your age: 20 you can vote
Enter your age: 14 you cant vote
Bash if-elif-else Statement
The if-elif-else statement is a type of conditional statement which can check more than two expressions and run the corresponding statements.
The statement is the extension of the if-else statement. When both the if and else parts do not satisfy the condition, we can still specify the statements to run.
Let’s understand the syntax below.
if expression1 then statement1 elif expression2 then statement2 else statement3 fi
Here, the if statement checks expression1.
If the condition matches, then statement1 will execute, and the control goes to the fi statement to end the conditional check. But, if the condition does not match, expression2 is evaluated. If expression2 is true, the statement2 will execute, and the conditional check will end. If the condition in the expression2 also does not match, then statement3 will execute.
You can integrate the multiple elif statement to check the various conditions.
The following example finds whether the entered number is negative, positive, or 0 using the if-elif-else statement.
read -p "Enter a number: " num if $((num)) -gt 0 then echo "positive" elif $((num)) -lt 0 then echo "negative" else echo "zero" fi
Firstly, in the if part, the number is checked if greater than 0. Next, in the elif part, the number is checked if it is less than 0. If both conditions fail, the else part executes.
Let’s see how the above script works with different inputs.
Enter a number: 22 positive
Since 22 is greater than 0, the first condition is met, and the message is displayed.
Enter a number: -5 negative
Since -5 is less than 0, the second condition is met, and the respective message is displayed.
Enter a number: 0 zero
Since 0 is neither less than 0 nor greater than 0, the first two condition fails. So, the statement in the else part is executed.
Nested if Statements
The if statement can also be written inside another if statement. This kind of if statement is called a nested if statement. The first if statement should be true to enter the nested statement. Let’s look at the script below to understand the concept more clearly.
The script below will first check if the pass marks are obtained. If it is the case, the script will check the grade and tell whether the student has obtained an A grade. If the pass mark is not obtained, it will tell the student failed.
read -p "Enter marks: " marks if $((marks)) -gt 39 then if $((marks)) -gt 89 then echo "grade A" else echo "less than grade A" fi else echo "failed" fi
The following are the results of testing the above script with different inputs.
Enter marks: 96 grade A
The first if condition evaluates true for the marks 96 and enters the nested if statement. Grade A is shown since 96 is also greater than 89.
Enter marks: 88 less than grade A
For the marks 88, the first if condition evaluates true, and the execution goes to the nested if statement. Since 88 is not greater than 89, the nested if condition fails, and the nested else part is executed.
Enter marks: 39 failed
For the marks 39, the initial if statement fails, so the execution does not go to the nested part.
This tutorial introduced the if, if-else, and if-elif-else statements. Thus, you learned how to use these different conditional statements to perform your desired conditional checks.