The if-else is one of the most useful conditions you can apply in bash scripting as it will let you decide certain aspects of your code that you want to execute. It comes under the category of conditional statements and has many variations.
This article will provide the detailed usage of the if-else statements in bash with examples. The content offered in this post is as follows:
- What is Bash Scripting?
- How to Create a Bash Script
- What are if-else Statements, and Why Use Them?
- Bash if-else examples
We’ll now explain each of the above below.
What is Bash Scripting?
The Bash Script is a text file containing the commands to be executed. Moreover, it offers the programming features to implement the control structures (conditional statements) and other core concepts.
A bash script can be executed using one of the following syntaxes:
$ bash <File-Name.sh> or $ ./<File-Name.sh>
How to Create a Bash Script?
To create a bash script file, open a terminal and use any text editor (we recommend using nano editor), as seen below.
Copy and paste the below line before doing anything; start typing or pasting your script, press “Control + s” to save, enter the file name, and add “.sh” at the end.
The above image mentioned what you must do to create a bash script file. We’ll now move on to the next section, where the bash if else, is explained.
What are If-else Statements in Bash, and Why Use Them?
Programming is complicated without conditional statements; you can do it, but using them is simplified, and why write a hundred lines when your work can be done in 5-20 lines? Well, these if-else statements are for your rescue for the following reasons.
- Code simplification and optimization
- Highly customizable conditions
- Better decision-making strategy
But what exactly are these if-else statements? Here’s the syntax of a basic if-else statement.
if <Condition>; then <Command> elif < Condition >; then <Command> else <Command> fi
Here we have three things:
- The condition, for example, 10 is greater than one (10>1)
- the elif or else-if, which is second after the first if isn’t true
- Finally, the else, in which is where the command is to be done when there is nothing true in both if statements.
To help you understand, we will explain through examples in the next section.
Examples of Bash if-else
This section provides a list of examples to practice the usages of the if-else statements in bash:
Example 1: Grade Papers based on Points Scored
To grade the papers, we’ve set criteria where points above 80 will grant you an A+ grade, while points from 60 to 80 will give you B+, and anything below 60 is an F.
The bash script’s code is written below:
#!/bin/bash read -p "Enter the points scored: " ps if [ $ps -gt 80 ] then echo "A+" elif [ $ps -ge 60 ] then echo "B+" else echo "F" fi
We’ve added two conditions here, and either one is true. For example, if the points are above 80, the grade is A+, and below 80, but above 60 is B+, while anything below 60 is F.
We’ll execute it in the terminal like this.
$ bash marks.sh
In the above command, our file name was “marks.sh”, so change it according to the file you’ve named.
Example 2: Check if You are a Root User or Not
Using a bash script, you can also execute commands like we’d check if the current user is root or not, like this.
#!/bin/bash if [ $(whoami) = 'root' ]; then echo "You are root" else echo "You are not root" fi
In the above script, we’ve run the “whoami” command that would tell you if you’re a root or not, and there are several other commands that can be used in scripting by using a “$” before them.
The above-created script file can be executed as seen below.
$ bash RootTest.sh
In the above script, we’ve named our file “RootTest.sh” and you are to name your file accordingly.
Example 3: Compare Three Numbers
In this example, we’d compare three numbers and print which one is greater using a bash script.
#!/bin/bash echo -n "Enter first number to compare: " read num1 echo -n "Enter second number to compare: " read num2 echo -n "Enter third number to compare: " read num3 if [[ $num1 -ge $num2 ]] && [[ $num1 -ge $num3 ]] then echo "$num1 is the largest number." elif [[ $num2 -ge $num1 ]] && [[ $num2 -ge $num3 ]] then echo "$num2 is the largest number." else echo“$num3 is the largest number” fi
And now, we’ll execute this script like this.
$ bash GreaterTest.sh
In the above script, three numbers are compared, so the user must input all three to know the greatest number among the three.
Additional Details Regarding Conditional Statement
This command is to be executed to learn more about conditional statements and how to use them.
$ help test
This long list goes way down.
This is how you can use if else in bash.
The conditional statements (if-else) are pillars of every programming language, and the same is the case with bash scripting, where if-else reduces a significant amount of code. This guide has listed the details of the if-else conditions in bash with examples.