Bash If
In this topic, we will understand how to use if statements in Bash scripts to get our automated tasks completed.
Bash if statements are beneficial. They are used to perform conditional tasks in the sequential flow of execution of statements. If statements usually allow us to make decisions in our Bash scripts. They help us to decide whether or not to run a piece of codes based upon the condition that we may set.
Basic if Statements
A basic if statement commands that if a particular condition is true, then only execute a given set of actions. If it is not true, then do not execute those actions. If statement is based on the following format:
Syntax
if [ expression ];
then
statements
fi
The statement between then and fi (If backwards) will be executed only if the expression (between the square brackets) is true.
Note: Observe the spaces used in the first line and a semicolon at the end of the first line; both are mandatory to use. If conditional statement ends with fi.
For using multiple conditions with AND operator:
if [ expression_1 ] && [ expression_2 ];
then
statements
fi
For using multiple conditions with OR operator:
if [ expression_1 ] || [ expression_2 ];
then
statements
fi
For compound expressions with AND & OR operators, we can use the following syntax:
if [ expression_1 && expression_2 || expression_3 ];
then
statements
fi
Following are some examples demonstrating the usage of if statement:
Example 1
In this example, take a user-input of any number and check if the value is greater than 125.
!/bin/bash
read -p " Enter number : " number
if [ $number -gt 125 ]
then
echo "Value is greater than 125"
fi
Output
If we enter the number 159, then the output will look like:
Example 2
In this example, we demonstrate the usage of if statement with a simple scenario of comparing two strings:
#!/bin/bash
# if condition is true
if [ "myfile" == "myfile" ];
then
echo "true condition"
fi
# if condition is false
if [ "myfile" == "yourfile" ];
then
echo "false condition"
fi
Output
Example 3
In this example, we demonstrate how to compare numbers by using the if statement:
#!/bin/bash
#if condition (greater than) is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
fi
#if condition (greater than) is false
if [ 3 -gt 10 ];
then
echo "3 is not greater than 10."
fi
#if condition (lesser than) is true
if [ 3 -lt 10 ];
then
echo "3 is less than 10."
fi
#if condition (lesser than) is false
if [ 10 -lt 3 ];
then
echo "10 is not less than 3."
fi
#if condition (equal to) is true
if [ 10 -eq 10 ];
then
echo "10 is equal to 10."
fi
#if condition (equal to) is false
if [ 10 -eq 9 ];
then
echo "10 is not equal to 9"
fi
Output
Example 4
In this example, we will define how to use AND operator to include multiple conditions in the if expression:
#!/bin/bash
# TRUE && TRUE
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "Conditions are true"
fi
# TRUE && FALSE
if [ "mylife" == "mylife" ] && [ 3 -gt 10 ];
then
echo "Conditions are false"
fi
Output
Example 5
In this example, we will define how to use OR operator to include multiple conditions in the if expression:
#!/bin/bash
# TRUE || FALSE
if [ 8 -gt 7 ] || [ 10 -eq 3 ];
then
echo " Condition is true. "
fi
# FALSE || FALSE
if [ "mylife" == "yourlife" ] || [ 3 -gt 10 ];
then
echo " Condition is false. "
fi
Output
Example 6
In this example, we will define how to use AND and OR to include multiple conditions in the if expression:
#!/bin/bash
# TRUE && FALSE || FALSE || TRUE
if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];
then
echo "Condition is true."
fi
# TRUE && FALSE || FALSE
if [[ 8 -eq 8 && 8 -gt 10 || 9 -lt 5 ]];
then
echo "Condition is false"
fi
Output
Options for If statement in Bash Scripting
If statement contains many options to perform a specific task. These options can be used for file operations, string operations, etc. Following are the some mostly used options:
Nested If
You can apply as many 'if statements' as required inside your bash script. It is also possible to use an if statement inside another 'if statement'. It is known as Nested If Statement.
Example
In this example, we will find "if a given number is greater than 50 and if it is an even number" by using nested if expression.
!/bin/bash
Nested if statement
if [ $1 -gt 50 ]
then
echo "Number is greater than 50."
if (( $1 % 2 == 0 ))
then
echo "and it is an even number."
fi
fi
Output
If we input an argument value as 100, then the output will look like:
In this topic, we will understand how to use if-else statements in Bash scripts to get our automated tasks completed.
Bash if-else statements are used to perform conditional tasks in the sequential flow of execution of statements. Sometimes, we want to process a specific set of statements if a condition is true, and another set of statements if it is false. To perform such type of actions, we can apply the if-else mechanism. We can apply the condition with the 'if statement'.
Bash If Else Syntax
A syntax of if-else statement in Bash Shell Scripting can be defined as below:
if [ condition ];
then
<if block commands>
else
<else block commands>
fi
Important Points to Remember
We can use a set of one or more conditions joined using conditional operators.
Else block commands includes a set of actions to perform when the condition is false.
The semi-colon (;) after the conditional expression is a must.
Check out the following examples demonstrating the use of the if-else statement in Bash Script:
Example 1
Following example consists of two different scenarios where in the first if-else statement, the condition is true, and in the second if-else statement, the condition is false.
!/bin/bash
when the condition is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
else
echo "10 is not greater than 3."
fi
when the condition is false
if [ 3 -gt 10 ];
then
echo "3 is greater than 10."
else
echo "3 is not greater than 10."
fi
Output
In the first if-else expression, the condition ( 10 -gt 3 ) is true and so the statement in the if block is executed. Whereas in the other if-else expression, the condition ( 3 -gt 10 ) is false and so the statement in the else block is executed.
Example 2
In this example, we explained how to use multiple conditions with the if-else statement in Bash. We use bash logical operators to join multiple conditions.
!/bin/bash
When condition is true
TRUE && FALSE || FALSE || TRUE
if [[ 10 -gt 9 && 10 == 9 || 2 -lt 1 || 25 -gt 20 ]];
then
echo "Given condition is true."
else
echo "Given condition is false."
fi
When condition is false
TRUE && FALSE || FALSE || TRUE
if [[ 10 -gt 9 && 10 == 8 || 3 -gt 4 || 8 -gt 8 ]];
then
echo "Given condition is true."
else
echo "Given condition is not true."
fi
Output
Bash If Else Statement in a Single Line
We can write complete 'if-else statement' along with the commands in a single line. You need to follow the given rules to use if-else statement in a single line:
Use a semi-colon (;) at the end of statements in if and else blocks.
Use spaces as a delimiter to append all the statements.
An example is given below demonstrating how to use if-else statement in a single line:
Example
!/bin/bash
read -p "Enter a value:" value
if [ $value -gt 9 ]; then echo "The value you typed is greater than 9."; else echo "The value you typed is not greater than 9."; fi
Output
When we enter a value as 25, then the output will look like:
Bash Nested If Else
Just like nested if statement, the if-else statement can also be used inside another if-else statement. It is called nested if-else in Bash scripting.
Following is an example explaining how to make use of the nested if-else statement in Bash:
Example
!/bin/bash
read -p "Enter a value:" value
if [ $value -gt 9 ];
then
if [ $value -lt 11 ];
then
echo "$value>9, $value<11"
else
echo "The value you typed is greater than 9."
fi
else echo "The value you typed is not greater than 9."
fi
Output
If we enter 10 as value, then the output will look like this:
Top comments (0)