if statement in a Bash script is the most basic way to use a conditional statement.
In simple terms, these conditional statements define “if a condition is true, then do that, otherwise do this instead.” The
if statements become more complex when you nest them together, or in other words put one
if statement inside of another
if statement. You can make the nest as deep as you want, though it will continue to grow in complexity.
In this tutorial you will learn:
- How to structure a nested if statement in a Bash script
- Real example of nested if statements to try on your own system
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Software||Bash shell (installed by default)|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Structure of a nested if statement
First, let’s go over how a nested
ifstatement is structured in a Bash script. This will familiarize you with the syntax so you can easily interpret the coming examples, and eventually write your own from scratch.
#!/bin/bash if EXPRESSION1; then STATEMENT1 if EXPRESSION2; then STATEMENT2 fi fi
This is the most basic form of a nested
if statement. Let’s go over what is happening here:
- The first
ifstatement checks if an expression is true. If it is, it proceeds with the first statement.
- After the first statement, the script then checks if yet another expression is true. If it is, it proceeds with the second statement.
- But if the condition is not matched for the second
ifstatement, only the first statement is executed and then the script escapes from the entire nested
- If the first expression does not match, then there is no chance of entering into the nested
ifstatement to check for the second condition or any other subsequent conditions.
- Remember that the second
ifstatement needs to be terminated before the first one, since it is nested
If any of this does not make sense at first, do not worry. Looking at the example below will help you visualize the process better.
Bash Scripting: Nested if statement example
Check out the example below to see how to use nested
if statements within a Bash script.
It is normally advantageous to use a case statement rather than a bunch of nested
The script below will first check to see if it is a weekday (Mon-Fri). If it is, it will then check to see if it is morning or night time (AM or PM). But, if the first
if statement is not met because it is the weekend (Sat or Sun), then the second
if statement will not get executed at all.
#!/bin/bash # Determine the day of week and store it inside the $day variable day=$(date +"%u") # Determine if it is morning or night and store it in the $time variable time=$(date +"%p") # Check if the day of the week is between 1-5 (Mon-Fri) if [ $day -le 5 ]; then # if it is a weekday, echo the text below echo "today is a weekday" # now determine if it is morning or night time if [ $time == "AM" ]; then echo "it is morning" else echo "it is night" fi else # if the first condition was not met, execute the following command echo "today is the weekend!" fi
Here is the output when we execute the script:
$ date Fri 25 Feb 2022 09:55:14 PM EST $ ./test.sh today is a weekday it is night
The point of a nested
ifis that the second
ifstatement is only used if the first
ifstatement is true. In this case, our script only checks the time of day if it first determined that the day of the week is Mon-Fri. We have left comments in the script to make this easier to digest.
In this tutorial, you learned how to use a nested
if statement in Bash scripting on a Linux system. This type of conditional statement has a niche use, since usually it is better to use
case statements. If your nest is only two
if statements deep, then this it is usually easy to maintain and understand what is going on, as we showed in the example here.