In bash scripting, conditional statements help in checking whether a specific condition is true or false. When the condition is true, the shell script runs or executes a block of code that is stipulated by the script. Otherwise, the script skips executing the code and jumps to the next conditional statement.
In bash scripting, conditional statements include if, if-else, if-elif-else and nested if statements. In this guide, we will explore how these conditional statements are used in the execution of scripts.
The if statement
In its simplest form, the ‘if’ statement takes the following syntax.
if [[ conditional_statement ]] then command fi
The conditional if statement begins with the if keyword. This is followed thereafter by a conditional statement which is then followed by the then keyword which precedes the command to be executed if the conditional statement holds true.
So, if the condition_statement holds true, then the command is executed. If it evaluates to False, nothing happens and the command is entirely ignored and the script exits.
Let us take an example of a bash script that checks if a score provided is greater than 80.
$ vim check_score.sh
Copy and paste the script.
#!/bin/bash echo -n "Please Enter a score: " read score if [[ $score -gt 80 ]] then echo "You have passed the exam!" fi
Take careful note of the indentation. Assign execute permissions using the chmod command as follows:
$ chmod +x check_score.sh
Then run the script.
If you enter a value that is greater than 80, the conditional statement holds true and the echo message is printed to the stdout
However, if the value provided for the score is less than 80 – the conditional statement evaluates to false and nothing happens. The script simply exits.
The if-else statement
In the previous shell script, only one command was executed after the conditional statement evaluated to True. When the condition evaluated to False, nothing was printed and the script just exited. The if-else statement helps us to execute a command should a condition evaluate to false. The else statement is then followed by a command or statement should the condition return False.
if [[ conditional_statement ]] then command else command fi
Let’s modify the previous shell script and add an else statement
#!/bin/bash echo -n "Please Enter a score: " read score if [[ $score -gt 80 ]] then echo "You have passed the exam!" else echo "You have failed the exam, please try again" fi
Here, the shell script executes the block of code after the else statement only if the first condition evaluates to false – that is if the score provided is less than 80.
In the first instance, the score provided is 90, which is greater than 80, and hence the first statement in the script is executed and the script exits.
In the second case, however, 70 is provided, and since the value is less than 80, the script skips the first statement since it evaluates to false and proceeds to execute the second statement after the else statement.
The if-elif-else statement
So far, we have checked for one condition that evaluates to either True or False. The elif (else -if ) statement is used where there are several conditional statements that have different outcomes. If one condition evaluates to False, it proceeds to check if other conditions are True. Here’s a simple syntax for the if-elif-else statement.
if [[ conditional_statement_1 ]] then command_1 elif [[ conditional_statement_2 ]] then command_2 else command_3 fi
Consider the script below
#!/bin/bash echo -n "Please Enter a score: " read score if [[ $score -ge 90 ]] then echo "Excellent!" elif [[ $score -ge 70 ]] then echo "Congratulations! Job well done!" elif [[ $score -ge 50 ]] then echo "Fairly done. There's more room for improvement!" else echo "Poorly done" fi
In this example, if the score is greater than or equal to 90, it prints the message Excellent! However, if the score is less than 90 but greater than or equal to 70, it prints Congratulations! Job well done!.
If the score is less than 70, but greater than or equal to 50 it prints out the message Fairly done. There’s more room for improvement!
And finally, if the score is less than 50, it prints out Poorly done.
Nested if statement
A nested if statement comprises an if statement inside another if statement. when one condition holds true, the script proceeds to check the next condition. In the demonstration below, we are going to take the input values of 3 numeric values and check which among them is the largest.
#!/bin/bash read -p "Please enter the value of j:" j read -p "Please enter the value of k:" k read -p "Please enter the value of l:" l if [ $j -gt $k ] then if [ $j -gt $l ] then echo "j is greatest" else echo "l is greatest" fi else if [ $k -gt $l ] then echo "k is greatest" else echo "l is greatest" fi fi
That was a high-level overview of conditional statements in bash scripts. As you have seen conditional statements are used to determine the outcome of a script based on set conditional expressions. Hopefully, you now have a basic foundation of conditional statements.