You are currently viewing Shell Scripting – Conditions
Shell Scripting – Conditions

Shell Scripting – Conditions

Shell Scripting – Conditions

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed loops in shell.

https://cloudaffaire.com/shell-scripting-loops/

In this blog post, we will discuss conditions. Like any other scripting language, the shell also provides conditions. You can use conditions in a shell script for decision making. In shell we mainly use IF conditional statement for decision making. Shell supports if, if else and if elif else conditional statements. Apart from if, case can also be used as a conditional statement.

Shell Scripting – Conditions:

Different ways to use conditional statement:

There are different ways you can define the conditional expression in shell under if and each has different pros and cons associated with it.

if:

if is the simplest conditional block that you can define in a shell script. If evaluates the condition and if the condition is true subsequent command gets executed.

if else:

Unlike if, if else evaluates both true and false outcomes of the given condition.

if elif else:

Using if elif else, you can evaluate multiple conditions in your shell script. Multiple elif blocks can be defined in if elif else block depending upon your requirement.

Nested if:

You can include multiple if, if else and if elif else statements within another if, if else and if elif else statement which is also called nesting.

Case:

Case statement can also be used to evaluate conditions in a selection statement.

test:

You can also test inbuilt command to evaluate a conditional statement.

Hope you have enjoyed this article, in the next blog post, we will discuss redirections.

 

 

Leave a Reply