Bash if..else Statement
Bash if-else statement or conditional statement is the most fundamental concept in any programming language that provides the facility to execute code conditionally.
It is useful when we want to execute a piece of code only if certain conditions meet and execute some when the condition does not meet.
The if-else statement is used for this purpose and has several other forms such as if, if-else, if-elif-else, nested if, etc.
What is if..else conditions and statements in Bash?
To help you easily understand vocabularies used in this article, we have created a table which covers the meaning of if else in bash, conditions and statements.
Vocabulary |
Meaning |
Example |
if |
A keyword that starts a conditional statement. It is followed by a condition in [] brackets and a then keyword. |
if [ condition ] then |
condition |
A test that evaluates to true or false. |
$age -gt 18 |
statement |
Block of code that gets executed depending on the result of a condition. |
echo "You are not eligible" |
elif |
These are used to add more conditions and statements to an if statement. |
elif [ condition2 ]; then |
nested if |
Creating a if statement inside an if statement. Or in other words, a conditional statement inside in the statement of a conditional statement. |
if [ condition1 ]; then
if [ condition2 ]; then |
else |
An alternative block of code to be executed if all if and elif condition are false. |
else echo "access denied" |
We will learn to use all these conditional flows in this article. So, let's get started.
1. if Statement
This is the simplest use of a conditional statement that executes only when the specified condition is true. The general syntax of this statement is.
if [ CONDITIONAL-COMMAND ]
then
STATEMENTS
fi
The CONDITIONAL-COMMAND
is a condition, and the if
statement executes only when this condition is true.
The then
keyword is associate with “if and elif statement” and enclose the statements that execute when "if" is true.
Always remember to add fi
at the end of conditional statements.
Example: if Statement
In this example, first, a statement reads the user input and then validates that in the if conditional statement. If the user input is less than 20 then the if condition will execute.
#!/bin/bash
echo -n "Enter a number: "
read VAL
if [[ $VAL -lt 20 ]]
then
echo "The value is less than 20."
fi
Here, the condition is [ $VAL -lt 20 ]
which checks if variable $VAL
is less than 20.
Save this code into a file with .sh extension, such as myscript.sh
, and execute by using the following command.
$ bash myscript.sh
It will ask for the user input and if the input is less than 20 then the output will be:
The value is less than 20.
2. if-else statement
This statement is an additional version of the if-statement and provides an else statement that executes when the if statement is false.
So, if you want to execute some statements when the if condition fails. The general syntax of this statement is:
if [ CONDITIONAL-COMMAND ]
then
STATEMENTS
else
STATEMENTS
fi
Let's understand by an example.
Example: if else in bash
In this example, first, a statement reads the user input and then validates that in the if conditional statement. if the user input is less than 20 then the if condition will execute, if not, then the else statement executes.
#!/bin/bash
echo -n "Enter a number: "
read VAL
if [[ $VAL -lt 20 ]]
then
echo "The value is less than 20."
else
echo "The value is equal or greater than 20."
fi
It will ask for the user input and if the input is greater than or equal to 20 then else block will be executed and the output will be:
The value is equal or greater than 20.
Another great example is to use if else in bash script to check if a file exist.
3. if-elif-else Statement
This statement is used to test multiple conditions in a row and exit the control if any of the conditions is true.
It is also known as the if-else ladder in some programming languages. The general syntax of this statement is:
if [ CONDITIONAL-COMMAND1 ]
then
STATEMENTS1
elif [ CONDITIONAL-COMMAND2 ]
then
STATEMENTS2
else
STATEMENTS3
fi
The next elif statements execute only when the if condition or any preceding elif is false.
Example: if-elif-else Statement
Here, if the user enters 20 then the if condition fails and the control goes to elif condition where the condition is true and the statement executes.
#!/bin/bash
echo -n "Enter a number: "
read VAL
if [[ $VAL -lt 20 ]]
then
echo "The value is less than 20."
elif [[ $VAL -eq 20 ]]
then
echo "The value is equal to 20."
else
echo "The value is equal or greater than 20."
fi
It will ask for the user input and if the input is equal to 20 then elif block will be executed and the output will be:
The value is equal to 20.
4. Nested if Statement
Bash allows using any number of if statements inside an if statement, that means we can use a nested if statement to test multiple conditions.
It is helpful when we have to check multiple conditions inside an if statement. The general syntax of this statement is:
if [ CONDITIONAL-COMMAND1 ]
then
if [ CONDITIONAL-COMMAND2 ]
then
STATEMENTS1
else
STATEMENTS2
fi
else
STATEMENTS3
fi
The nested-if statement executes only if the first or parent if
condition pass.
Example: Nested if else in bash
In this example, we are taking three input values from the user and test which one is smaller by using the nested-if statement.
#!/bin/bash
echo -n "Enter first number: "
read VAL1
echo -n "Enter second number: "
read VAL2
echo -n "Enter third number: "
read VAL3
if [[ $VAL1 -lt $VAL2 ]]
then
if [[ $VAL1 -lt $VAL3 ]]
then
echo "$VAL1 is smallest Number"
else
echo "$VAL3 is smallest Number"
fi
fi
It will ask for user input three times and if the inputs are 20, 50, 40 respectively then the output will be:
20 is smallest Number
FAQs
Although, we have covered a lot about how you can use Bash if...else statements. Here are some possible questions related to their use.
Q. Is it possible to use Bash if…else statements in one line?
Yes, you can use if…else statements in one line in Bash by separating the keywords and statements with semicolons (;
). For example:
if [ condition ]; then echo "true"; else echo "false"; fi
This will print true or false depending on the condition.
In other word, just add semicolons after everything except if, then, elif, else and fi.
Q. How to use exit codes with Bash if…else statements?
You can use exit codes in if…else statements in Bash to check status of previous command or a script. The exit code, by convention, value 0 means success and any other value (1-255) means failure. The exit code of the previous command is denoted by $?.
command
if [ $? -eq 0 ]; then
echo "command succeeded"
else
echo "command failed"
fi
Q. Can I use arithmetic operators in Bash if…else statements?
You can use arithmetic operators such as -eq, -ne, -lt, -gt, -le, and -ge to compare numeric values in Bash if…else statements. For example:
if [ $a -eq $b ]
The above code checks if variable $a
is equal to variable $b
.
Q. Can we nest if…else statements in Bash?
Yes, you can use nested if…else statements in Bash to create complex conditional logic as given below:
if [ condition1 ]; then
#statements1
elif [ condition2 ]; then
if [ condition3 ]; then
# (nested) statements2
else
# (nested) statements3
fi
else
#statements4
fi
This will execute different blocks of statements depending on the evaluation of the conditions.
The Bash script uses conditional statements such as if, if-else, if-elif, and nested if to create conditional programs that execute based on the specified condition. You can also use if else in bash script inside for loop. Happy Shell scripting!