Bash if/else Statement - Shell Scripting



Let's continue our discussion on the Bash scripting, In this post, we will look into how to write the conditional statements in Bash.

If you are new to Bash Scripting read our fundamentals post here.

Like any other programming language, Bash scripting also supports conditional statements, where a particular statement is executed when a specific or given condition satisfies.

Bash has its own unique way to deal with conditional statements. Before you dive into conditional statements, it is very important to know how Bash checks for a condition.

Exit Status In bash

Whenever you execute a command in a Bash terminal, If the command executes successfully it returns an exit status code 0. If it fails it will return an exit status code with a non zero value.

Bash uses the exit status code to determine if a condition is True or false.

Now, that you have an idea of how the conditional statement determines for true and false, Let's see the syntax of conditional statement if-else and elif.

if condition
then
    statement 1
    statement 2
else
    statement 1
fi

That was a simple if-else statement in Bash. We can also write an if-elif-else ladder:

if condition
then
    statement 1
    statement 2
elif condition 2
then
    statement 1
    statement 2
else
    statement 1
fi

The conditional statement starts with if and ends with fi. This indicates the entire conditional block.

Now, Let's talk about how we can write the conditions. In Bash, it provides us a unique way of specifying conditions.

We know that True and False are evaluated using the exit status of a command. If the command executed without any error, the exit code has a value of 0 which is considered as True. So, we use the test command to evaluate a conditional statement.

Like every other command available in the Linux, has its own flags, test command also has flags by which you can perform various operations like, comparing two variables and etc...

Before we discuss some flags and write a script, Let's first see the test command in action.

Here, I will use a flag, -lt which is "less than" and is used for comparing two numbers.

test 5 -lt 10



When you will press the enter, you will not see any output! this is because exit status is only seen by a particular command which is $?

So, When I will write,

echo "The exit status of test 5 -lt 10 is $?"



The $? is used to check the exit status of the command recently executed. Here we got an exit status of 0, which means our command is successfully executed because 5 is less than 10.

Now, Let's write a script which compares two numbers and find out which one is small. 

#!/bin/bash
a=10
b=5
if test $a -lt $b
then
    echo "$a is smaller than $b"
else
    echo "$b is smaller than $a"
fi

The output of the above program will be:

5 is smaller than 10

Now, As you have seen how we can evaluate a condition, It's time to tell you an important change.

The above test command can be replaced with [ ] square brackets. And you can also write the above code as:

#!/bin/bash
a=10
b=5
if [ $a -lt $b ]
then
echo "$a is smaller than $b"
else
echo "$b is smaller than $a"
fi
But, when you are writing like this, be careful with spaces. you have to give proper space as still the above expression is treated as commands.

[spaceVariableSpaceFlagsSpaceVariableSpace] This is the correct way to write.

Now, Let's discuss some flags.

String Operations

 Flagsusage  Explanation
 -z
 [ -z $string ]
 
    True if the length of the string is zero

 -n           
 [ -n $string ]
 
    True if the length is non zero

 =
 [ $a = $b ]
 
    True if both the string are same

!=
 [ $a != $b ]    

    True if both the string is not the same



Numeric Tests

 FlagsUsage Explanation 
-lt
[ $a -lt $b ] 
    
    True if a is less than b
 
 -gt
[ $a -gt $b ]

    True if a is greater than b
 
-eq
 [ $a -eq $b ]

    True If a is equal to b

 -nq
[ $a -nq $b ]
 
    True if a is not equal to b

 -le
 [ $a -le $b ]
 
    True if a is less than or equal to b

 -ge
 [ $a -ge $b ]

    True if a is greater than or equal to b
 


So, That's it... Now, you know how to write a conditional statement in bash. If you love this post do follow this blog and subscribe. Up again in the next post, we will discuss how to write loops in Bash. We will talk about while loops, for loops, and many more.


For now, peace out.
Thank you
Share and Subscribe.


Post a Comment

0 Comments