If statements in bash scripting
Hello. In this tutorial, we will understand and see a practical implementation of bash if scripting in Linux based environment.
1. Introduction
If statement in Linux allows making decisions in the bash scripts. They allow us to decide whether or not this code has to be run based on the conditions met. if statement can be combined with a loop that could allow a user to write complex queries.
1.1 Setting up Linux environment
To set up the Linux environment I have created an ec2 instance on the AWS cloud. You are free to create your own Linux environment.
2. if statements in bash scripting
I will be using Visual Studio Code as my preferred IDE for writing the shell scripts.
2.1 Basic if statement
It simply says that if a particular condition evaluates to truly operate. If not true skip those operations. Represented by the syntax –
Syntax
1 2 3 4 | if [ some_condition ] then commands... fi |
Let us understand this with a simple example. The below script will print the x is greater than y
message on the console as the given condition evaluates to true.
Example
01 02 03 04 05 06 07 08 09 10 11 | #!/bin/bash # basic if statement example x=20 y=10 if [ $x -gt $y ] then echo "x is greater than y" fi |
2.2 Nested if statement
Nested if bash scripting is an example where multiple if statements are necessary. Represented by the syntax –
Syntax
1 2 3 4 5 6 7 8 9 | if [ condition ] then commands... else if [ condition ] then commands... fi fi |
Let us understand this with a simple example. The script will print the x is greater than or equal to 30
message on the console as the variable x
is not greater than 50 but it is equal to 30. In here the if condition inside of the else block will be executed.
Example
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | #!/bin/bash # nested if statement example x=30 if [ $x -gt 50 ] then echo "x is greater than 50" else if [ $x - ge 30 ] then echo "x is greater than or equal to 30" else echo "Cannot perform calculation" fi fi |
2.3 if-else statement
If-else statement in bash scripting is helpful when a user wants to perform a set of commands when the condition evaluates to false. Represented by the syntax –
Syntax
1 2 3 4 5 6 | if [ condition ] then commands... else commands... fi |
Let us understand this with a simple example. The script with print the y is greater than x
message on the console as the if condition will evaluate to false.
Example
01 02 03 04 05 06 07 08 09 10 11 12 13 | #!/bin/bash # if else statement example x=10 y=15 if [ $x -gt $y ] then echo "x is greater than y" else echo "y is greater than x" fi |
2.4 if-elif-else statement
To include a series of conditions in a bash script we use this approach. Represented by the syntax –
Syntax
1 2 3 4 5 6 7 8 9 | if [ condition ] then commands... elif [ condition ] then commands... else commands... fi |
Let us understand this with a simple example. The script will evaluate the multiple conditions and print the result on the console once the condition is evaluated.
Example
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | #!/bin/bash # if elif else statement example x=10 y=15 if [ $x -gt $y ] then echo "x is greater than y" elif [ $x -lt $y ] then echo "x is less than y" else echo "x is equal to y" fi |
2.5 boolean operators
In bash scripting, boolean operators play an important role when we have to perform logic on multiple conditions. Bash scripting supports two operators i.e. and (&&
) and or (||
). Let us understand this with a simple example.
Example
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/bin/bash # boolean operator example # or operator example echo "or operator example" name= "James" if [[ $name == "James" || $name == "Rose" ]] then echo "correct name found!" else echo "incorrect name found!" fi echo # and operator example echo "and operator example" x=21 y=9 if [[ $x == "21" && $y == "9" ]] then echo "John is $x and Adam is $y" else echo "John is $x and Jessie is $y" fi |
2.6 case statements
Bash scripting allows a way to perform an evaluation based on the variable matching a series of conditions. The case statement comes as an alternative to multiple if, elif, and else statements. Represented by the syntax –
Syntax
1 2 3 4 5 6 | case variable in pattern1) ;; pattern2) ;; esac |
Let us understand this with a simple example. The script will print the result on the screen based on a pattern that matches the variable.
Example
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | #!/bin/bash # case operator example fruits= "apple" case "$fruits" in #case 1 "apple" ) echo "you like apple" ;; #case 2 "mango" ) echo "you like mango" ;; #case 3 "pear" ) echo "you like pear" ;; #default *) echo "you don't like fruits" ;; esac |
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we saw the implementation of bash if scripting. You can download the source code from the Downloads section.
4. Download the Project
This was a tutorial for bash if scripting in a Linux environment.
You can download the full source code of this example here: if statements in bash scripting