Updated April 10, 2023
Introduction to If Statement in Shell Scripting
“If” statement in a shell scripting is essentially a type of Bash scripting and this type of statement essentially helps us in making a decision on the basis of a condition. So, before we jump on to what “If” statements actually are, let us look into the preliminary topic of Bash Scripting. Let us look into an analogous situation where a script in a theatre act or a play mentions about the chronological order of what needs to be done in the act. Similar to that a script for computer tells the computer of what needs to be done. And “Bash” in front of the scripting means that it tells Bash terminal of what needs to be done.
To start with the syntax of “If” statements let us first look at how many different types of “If” statements are possible in bash programming or scripting. Conditions for “if” statements are always mentioned in square brackets []. In our syntax anything mentioned in “< >” means that those are either the condition as per the requirement of user or problem statement or set of commands as desired by the user or the problem statement.
Syntax of If Statement in Shell Scripting
Let us start with the basic “If” statement and that is:
1. Basic “If” statement
Syntax:
if [<test_Condition>]
then
<set_of_commands_to_be_executed>
fi
2. Nested “If-Else” statement
Syntax:
if [<test_Condition_1>]
then
if [<test_Condition_2>]
then
<set_of_commands_to_be_executed>
else
<set_of_other_commands_to_be_executed>
else
if [<test_Condition_3>]
then
<set_of_commands_to_be_executed>
else
<set_of_other_commands_to_be_executed>
fi
fi
3. If-Elif-Else statement (Else-if ladder)
Syntax:
if [<test_Condition>]
then
<set_of_commands_to_be_executed>
elif [<test_Condition>]
then
<set_of_commands_to_be_executed>
elif [<test_Condition>]
then
<set_of_commands_to_be_executed>
else
<set_of_other_commands_to_be_executed>
fi
4. Boolean if condition
Syntax:
if [<test_Condition_1>] && [<test_Condition_2>]
then
<set_of_commands_to_be_executed>
fi
For this “&&” or “||” conditions can be used as a part of Boolean operations. This comes in very handy when 2 or more conditions need to be satisfied in the “if” condition.
5. Case/Switch Statement
Syntax:
case <variable_to_check_for_condition> in
<pattern_1_to_check_against_variable>)
< set_of_commands_to_be_executed >
;;
< pattern_2_to_check_against_variable >)
< set_of_commands_to_be_executed >
;;
esac
Flow Diagram
In this section, we would look into different flow diagrams for the respective “If” statements mentioned in the above section.
1. Basic “If” statement
2. Nested “If-Else” statement
Basic Nested:
3. Advanced Nested
4. If-Elif-Else statement
5. Boolean if condition
6. Case/Switch Statement
How if Statement works in Shell Scripting?
Below is the working of if statement works in shell Scripting:
1. Basic “If” statement
To understand how basic “if” statement work let us break down the syntax and explain them line by line.
if[<test condition>] ---- line 1
Then ----Line
<set_of_commands_to_be_executed> --- Line 3
fi --- Line 4
In the above break-up of the lines in Line 1 the if condition is checked for if that is satisfied. If not, the code will exit out of the loop and if the condition is satisfied then through Line 2 it will start executing commands mentioned in Line 3. There can be multiple commands and it will execute all of them if the code enters this block. Once done, the code will exit through Line 4.
2. Nested “If-Else” statement
In a Nested “If-Else” we can have multiple “If” checks inside another “If” block or “Else” block. Let us look at one example of the syntax and break it again line by line.
if [<test_Condition_1>] --- Line 1
Then --- Line 2
if [<test_Condition_2>] --- Line 3
then
<set_of_commands_to_be_executed> --- Line 4
else
<set_of_other_commands_to_be_executed> --- Line 5
else
if [<test Condition 3>] --- Line 6
then
<set_of_commands_to_be_executed> Line 7
else
<set_of_other_commands_to_be_executed> --- Line 8
if
fi
In the above example, in Line 1 the “If” condition is checked and if the condition is true; the code enters Line 3 through Line 2 block and again checks for “If” condition in Line 3. If the Line 3 condition is also satisfied the code enters the execution of commands in Line 4 or else moves to Line 5 set of commands. Now, if the “If” condition is Line 1 is adjudged as False then the code moves to check if the Test condition in Line 6 is true or not. If this condition is true, then it moves to the set of commands to be executed in Line 7 or else moves to Line 8.
3. If-Elif-Else statement
if [<test Condition>] --- Line 1
then
<set_of_commands_to_be_executed> --- Line 2
elif [<test_Condition>] --- Line 3
then
<set of commands to be executed> --- Line 4
elif [<test_Condition>] --- Line 5
then
<set_of_commands_to_be_executed> --- Line 6
else
<set of other commands to be executed> --- Line 7
fi
In an If-Elif-Else block in shell scripting, the condition for the “If” statement is checked in Line 1 and if it satisfies the condition moves to commands to be executed in Line 2. If the condition is not true in the previous Line 1 condition, another condition is checked for in Line 3 and if the condition is satisfied in Line 3 it moves to commands to be executed in Line 4. If this condition is also not fulfilled, another “elif” condition is checked in Line 5 and if it is true here, it will move to Line 6 commands. Else if none of the above “If” condition is satisfied commands in Line 7 are executed. The “Else” part which leads to commands in Line 7 is an optional command and the user can choose to not do anything if none conditions are satisfied.
4. Boolean if condition
if [<test_Condition_1>] && [<test_Condition_2>] --- Line 1
then
<set of commands to be executed> --- Line 2
fi
In the above example, in an AND operator in an “If” condition check if both the conditions are true it will move into commands in Line 2. Else if any one of the conditions returns a False value no commands would be executed. If there would an OR (||) operator in place of AND (&&) then, if anyone of the conditions is satisfied then commands in Line 2 would be executed. And only and only if both conditions are false nothing will be executed.
5. Case/Switch Statement
case <variable_to_check_for_condidtion> in ---Line 1
<pattern_1_to_check_against_variable>) --- Line 2
<set_of_commands_to_be_executed> --- Line 3
;;
<pattern_2_to_check_against_variable>) --- Line 4
<set_of_commands_to_be_executed> --- Line 5
;;
esac --- Line 6
In the above example, the variable mentioned in Line 1 is checked against different patterns mention in Line 2, Line 4 respectively. If the variable matches for a pattern in Line 2, Line 3 commands would be executed and if it matches a pattern in Line 4, commands in Line 5 would be executed. The double semicolon (;;) signifies the end of commands for a particular variable match block.
Conclusion
Finally, there are a lot of “If” statements in shell scripting and each of them supports solving a particular use case. One needs to be careful about selecting which one would best fit for the use case. Also, one should keep in mind that hybrid cases might also be formed by a combination of 2 or more scenarios described above. Ultimately, shell scripting is a powerful tool to automate the robotic workload and ease out human execution intervention.
Recommended Articles
We hope that this EDUCBA information on “If Statement in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.