BASH Programming

Bash if-else statements

In your day-to-day life, many things are associated with conditions. For example, when we decide, we first analyze its conditions, outcomes and finalize the decision. Conditional statements in any programming language shadow the same way, and they are core to every language. These statements are used to manage code execution flow and perform tasks based on true and false conditions.

The above chart is demonstrating the execution of the conditional statement. In programming jargon, we use the “if” word with a condition and specify the condition’s outcomes upon being true and false. Let’s check the basic syntax of a conditional statement in bash:

if [Condition]
then
    <Statement>
fi

if: Indicates the condition to be applied
then: If the condition is true, then execute the <statement>
fi: Closes the if statement

Let’s thoroughly understand the conditional statements with examples:

Bash if…then Example:

For this guide, we are using Vim editor; install it by executing the following command:

$ sudo apt install vim

Once the download and installation are finished, launch Vim editor in the terminal by typing:

$ vim

The basic if…then the example is given below:

#! /bin/bash
echo “Enter a number”
read number
if [ $number -lt 100 ]
then
echo “Your entered number is less than 100
fi

The above program is getting input from the user through the “read” command; the if statement is checking if the entered number is less than 100 or not. If the entered number is less than 100, then the “echo” statement will be executed; otherwise, the program will not give any output. In the next example, we will check how to get an output if the condition fails:

Bash if…then…else Example:

In the above example, if the condition is true, then the echo statement will be executed, now we will add “else” to get output when the “if” condition fails:

#! /bin/bash/
echo “Enter a number”
read number
if [ $number -lt 100 ]
then
echo “Your entered number is less than 100
else
echo “Your entered number is greater than 100
fi

Bash Nested if statement (if Elif):

To add more than one condition in the program, we use nested if statements. Let’s understand the nested if concept through an example:

#! /bin/bash/
echo “Enter a number from 1-20
read number
if [ $number -lt 10 ]
then
echo “Your entered number is less than 10
elif [ $number -le 20 ]
then
echo “Your entered number is greater than 10
else
echo “You entered number is not between 1-20
fi

The above code is demonstrating how to use nested if. The compiler will check both if and elif conditions and execute the statement satisfying the condition. The program will execute the “else” statement if both conditions are false.

Using multiple conditions with if…else:

To use multiple conditions with one if statement, we can use conditional operators:

  • AND operator “&&” execute “then” statement if all conditions are true
  • OR operator “||” execute “then” statement if any of the conditions are true

Let’s understand it with an example:

#! /bin/bash/
echo “Enter a number from 1-10
read number
if [ $number -ge 1 ] && [ $number -le 10 ];
then
echo “Your entered number greater than 1 and less than 10
else
echo “Your number is not between 1-10
fi

“then” statement will be executed when both conditions are true. In the above example, any number greater than 1 and less than 10 will satisfy both conditions.

#! /bin/bash/
echo “Enter a number”
read number
if [ $number -le 10 ] || [$number-le20];
then
echo “You have entered the correct number”
else
echo “Your entered the incorrect number”
fi

“then” statement will execute when either of the conditions is true. Any number that is less than 10, and 20 will be termed as a correct number greater than 20 will be incorrect because both conditions are not true.

Conclusion:

Like many other programming languages, bash scripting also uses conditional statements. If-else statement is a key part of any computer programming language. It helps to perform various functions based on the condition applied. This post thoroughly focuses on the conditional statement in bash scripting, its syntax, and various use of conditional statements with examples. It is one of the essential concepts for any developer to learn because these statements are building blocks of any algorithm.

About the author

Sam U

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.