The if is one of the decision making statement in Bash Shell scripting. It enables us executing one block of code (to perform certain action) among the two or more blocks.
As such, for taking a decision in any matter, there must be two or more options; so this is where if statement is for in computer programming.
Types of if statements in Bash Shell with examples
Depending on the scenario, you may use a different form of the if statement in Bash Shell scripting.
For example, if you want to check certain condition is true and not required to perform any action, task or display a simple message if the condition is false then you may use the simplest form of the if statement as follows:
if [ test_expression ] then command(s) to execute fi
There,
- The if keyword is followed by the condition you want to check.
- This is followed by “then” keyword.
- After that, commands/statements to execute if that condition is true.
- Finally, the “fi” keyword. Yes, this is not misspelled.
The example of simple if statement
In the example of showing how simple if statement works, we declared two variables and assigned them values.
In the if statement, we tested a condition whether two variable values are equal or not. If the condition is True, it will display a message, otherwise, no message is displayed or action is performed. Have a look:
The code:
#The demo of simple if #Two variables with numeric values x=5 y=5 #Using the if statement to check equality if [ $x == $y ] then echo "The values of both variables are equal!" fi
The result:
The values of both variables are equal!
As you can see, the values of variables x and y are 5, so the condition is true and a statement in the if statement displayed.
The following code will display nothing as the condition is False:
x=15 y=5 if [ $x <= $y ] then echo "x value is greater" fi
How to use Bash else with if statement?
You saw in the above example, no action is performed or any statement displayed when the condition was false.
In certain scenarios, you will require doing something; for example showing a message box to the user or action failure, display a message or perform some default action if the condition is false.
The general way of using the Bash else statement is as follows:
if [test_expression ] then < command(s) to execute when condition is true> else < command(s) to execute when condition is false> fi
An example of if..then..else.fi
See the example below with the output to learn how to use the if..then..else..fi in Bash scripting:
The code:
#The demo of if..else #Assign values to two variables x=500 y=1000 if [ $x == $y ] then echo "Values of both variables are equal" else echo "Values are not equal" fi
The result:
Values are not equal
You saw, as the condition is false, the statement inside the else statement executed.
Using the Bash elif statement
In many other languages, the elif statement is written as “elseif” or “else if”. The Bash elif statement enables us deciding from more choices than two; as we have seen in the above case.
For example, if a user enters the marks 90 or more, we want to display “Excellent”. For value more than or equal to 80 and less than 90, the displayed message is “Very Good”.
Similarly, for value more than or equal to 70 and less than 80, the displayed message should be “Good”. For less than 70, the message should be “Normal”.
So, how we can implement this by using the if statement. As such, we have seen the simple if statement allows us testing the single condition only. The else statement allows us performing an action if the if condition is False.
The answer is using the elif statement.
The example below shows using the elif statement. There, we have a variable which value is tested in the if, elif statements and if all are False, the else part should execute and display a message. Have a look:
# A demo of elif statement x=15 if [ $x -eq 10 ] then echo "The value is equal to 10" elif [ $x -eq 15 ] then echo "The value is equal to 15" elif [ $x -eq 20 ] then echo "The value is equal to 20" else echo "Some other value!" fi
The result:
The value is equal to 15
Similarly, the above scenario of marks can be translated into the if..elif..else statements as follows:
#Assigning marks to a variable marks=76 if (( $marks >= 90 && $marks <=100 )) then echo "Excellent" elif (( $marks >= 80 && $marks < 90)) then echo "Very Good" elif (( $marks >= 70 && $marks < 80)) then echo "Good" else echo "Normal" fi
The output:
Good
In the example above, you can see how the AND (&&) operator is used to get the result along with testing multiple conditions in the if statement.