The Bash Scripting is now a days mandatory language for most of the system admins/devops guys. so in upcoming articles we will shed light on the power and subtlety that is the Unix shell, I’d like to take a dive into just one of its many features: Bash Scripting – If Statement.
When coding, you might need to make decisions based on certain conditions. Conditions are expressions that evaluate to a boolean expression (true or false)Statements that help to execute different code branches based on certain conditions are known as conditional statements.if…else is one of the most commonly used conditional statements. Like other programming languages, Bash scripting also supports if…else statements. And we will study that in detail in this blog post.
In another way, If statements (and, closely related, case statements) allow us to make decisions in our Bash scripts. They allow us to decide whether or not to run a piece of code based upon conditions that we may set.
SYNTAX
When you are using a single if statement, the syntax is as follows: A basic if statement effectively says, if a particular condition is true, then perform a given set of actions. If it is not true then don’t perform those actions. If follows the format below:
The if statement is composed of the if keyword, the conditional phrase, and the then keyword. The fi keyword is used at the end of the statement. The COMMANDS gets executed if the CONDITION evaluates to True. Nothing happens if CONDITION returns False; the COMMANDS are ignored.. The basic syntax of an if statement is the following:
1 2 3 4 5 6 |
if [ condition ] then statement/actions fi |
The “[ ]” in the if statement above are actually a reference to the command test. This means that all of the operators that test allows may be used here as well. When you are using a multiple condition check with if statement, the syntax is as follows:
1 2 3 4 5 6 7 |
if [ condition ] ; then statement/actions elif [ condition ] ; then statement/actions else statement/actions fi |
- if >> Perform a set of commands if a test is true.
- elif >> If the previous test returned false then try this one.
- else >> If the test is not true then perform a different set of commands.
Note that the spaces are part of the syntax and should not be removed.
Example: Simple with IF statement
Let’s go through an example where we are comparing two numbers to find if the first number is the smaller one.
1 2 3 4 5 6 7 |
a=25 b=30 if [ $a -lt $b ] then echo "a value is less than b" fi |
Output: a value is less than b
Example: How to Use the if .. else Statement
Let’s see an example where we want to find if the first number is greater or smaller than the second one. Here, if [ $a -lt $b ] evaluates to false, which causes the else part of the code to run.
1 2 3 4 5 6 7 8 9 |
a=65 b=35 if [ $a -lt $b ] then echo "a is less than b" else echo "a is greater than b" fi |
Output: a value is greater than b
Example: How to Use if..elif..else Statements
To have comparisons, we can use AND -a and OR -o operators as well in the bash command. For performing the checks between two values, we can use AND -a and OR -o as well.
In this example, we will do the check on 3 values conditions:
1 2 3 4 5 6 7 8 9 10 11 12 |
if [ $a == $b -a $b == $c -a $a == $c ] then echo "All values are equal" elif [ $a == $b -o $b == $c -o $a == $c ] then echo "May be more than one value is equal" else echo "All numbers are not equal" fi |
Conclusion on Bash Scripting – If Statement
You can check the inputs based on conditions like if..else and make the code more dynamic. In this tutorial, hope you learned Bash Scripting – If Statement
I hope you found this tutorial helpful.
What’s your favorite thing you learned from this tutorial? Let me know on Twitter!
Leave A Comment