Conditionals Execution in Bash (Decision Making)
When you interact with your computer, you often make decisions:
If today is Friday, I’ll do a backup.
If the file exists, open it.
If the battery is low, plug in the charger.
What if your Bash scripts could do the same?
With conditionals in Bash, they can. Conditionals allow your scripts to make decisions based on specific situations instead of following a fixed set of instructions. In this guide, we’ll explore how to make your scripts smarter and more responsive.
From Sequential to Smart Scripts
Let’s begin with a simple Bash script:
#!/bin/bash
echo "Today is $(date)"
echo "Current directory: $PWD"
echo "Users currently logged in:"
w
This script runs commands sequentially without evaluating the context or conditions. It doesn’t care what day it is or who is logged in — it just runs.
Now, imagine needing a backup only on Fridays. Rather than executing the backup every time, the script can make a decision.
What is a Condition?
A condition is a true or false test that determines the next action in a script. For instance:
In Bash, test this with arithmetic:
echo $(( 5 < 2 ))
Output:
0
Using if Statements in Bash
Now that we understand conditions, let’s see how they’re used in Bash.
Basic Syntax
if condition
then
command1
command2
...
commandN
fi
Example: Backup on Friday
#!/bin/bash
day=$(date +%A)
if [ "$day" = "Friday" ]
then
echo "It's Friday. Time to back up your data."
tar -czf backup.tar.gz /home/user/data
else
echo "Today is not Friday. Backup skipped."
fi
Explanation
date +%A returns the current weekday (e.g., Monday, Friday)
if [ "$day" = "Friday" ] checks if today is Friday
If the condition is true, it runs the backup
Otherwise, it skips it
Understanding Comparison Operators
| Operator | Example | Description | Result | Output |
|---|
> | echo $((5 > 12)) | Is 5 greater than 12? | False | 0 |
== | echo $((5 == 10)) | Is 5 equal to 10? | False | 0 |
!= | echo $((5 != 2)) | Is 5 not equal to 2? | True | 1 |
< | echo $((1 < 2)) | Is 1 less than 2? | True | 1 |
== | echo $((5 == 5)) | Is 5 equal to 5? | True | 1 |
Real-World Script: Check if File Exists
#!/bin/bash
if [ -f /etc/resolv.conf ]
then
cp /etc/resolv.conf ~/resolv_backup.conf
echo "Backup created: ~/resolv_backup.conf"
else
echo "Error: /etc/resolv.conf does not exist."
fi
This script checks whether a file exists. If it does, a backup is created. Otherwise, an error message appears.
Using if, else, and fi in Bash
In everyday life, we often decide between two paths:
Bash allows similar logic using:
if — to evaluate the condition
else — to define an alternate action
fi — to close the conditional block
Syntax
if [ condition ]
then
# code when condition is true
else
# code when condition is false
fi
Example: Check if a File Exists, Create if Not
#!/bin/bash
if [ -f log.txt ]
then
echo "File log.txt found. Showing contents:"
cat log.txt
else
echo "File log.txt not found. Creating an empty one..."
touch log.txt
fi
Explanation
if [ -f log.txt ] checks whether the file exists
If it does, the script displays the contents
If not, it creates a new file using touch
fi ends the block
Conclusion
Mastering conditionals in Bash is a key step toward writing smarter, more efficient scripts. Whether you’re automating system backups, managing files, or building real-time tools, the ability to make decisions within your script gives you greater control and flexibility.
To build expertise in this area, enrolling in a structured and hands-on course is the smartest move. The Indian Institute of Embedded Systems (IIES) offers the best embedded systems course, carefully designed to combine theoretical knowledge with real-world application. IIES stands out as the best embedded systems course in Bangalore — and even better, it’s known as the best embedded systems course in Bangalore with placement, helping learners transition smoothly into high-demand job roles.
In addition, IIES is recognized as the best IoT training institute in Bangalore, delivering the best IoT course focused on practical, real-time projects, cloud integration, and hardware communication. Whether you’re a student or a working professional, joining the best IoT course in Bangalore with placement from IIES is a smart investment in your career, empowering you with future-ready skills in embedded systems, IoT, and intelligent automation.