Conditionals Execution in Shell scripting

Conditionals Execution in Shell scripting_1

INTRODUCTION

In today’s fast-paced digital world, writing shell scripts that adapt to changing conditions is more important than ever. Whether you’re backing up files, automating system tasks, or managing data, smart scripts save time and reduce errors. Instead of executing every command blindly, Bash scripts can use logic to respond to real-world situations.

Fortunately, conditionals in Bash allow you to build that intelligence. They help your script make decisions — just like you do when choosing to plug in a charger when your battery is low. In this guide, you’ll discover how to use if, else, and other control structures to write flexible and efficient Bash scripts. Whether you’re just starting out or brushing up your skills, this tutorial will make Bash conditionals easy to understand and apply.

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:

  • Is 5 greater than 2? → true

  • Is 10 equal to 3? → false

In Bash, test this with arithmetic:

echo $(( 5 < 2 ))

Output:

0
  • 0 means false

  • Any non-zero value means true

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

OperatorExampleDescriptionResultOutput
>echo $((5 > 12))Is 5 greater than 12?False0
==echo $((5 == 10))Is 5 equal to 10?False0
!=echo $((5 != 2))Is 5 not equal to 2?True1
<echo $((1 < 2))Is 1 less than 2?True1
==echo $((5 == 5))Is 5 equal to 5?True1

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:

  • If something happens, do Task A

  • Otherwise, do Task B

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.