Day 41 – Decision Making in Bash Scripting (Conditionals)

In today’s #DevOps journey, I dove deep into decision making in Bash scripting, which is one of the core building blocks for automating logic and flow.


🧠 What is Decision Making in Bash?

Decision making allows you to control the flow of execution based on conditions — like if a file exists, if a command was successful, or if user input matches something.


🧩 Bash Conditionals Overview

Here are all the main ways to implement decision making in Bash:


1️⃣ if statement

if [ condition ]; then
  # commands
fi

Example:

if [ -f file.txt ]; then
  echo "File exists"
fi

2️⃣ if-else statement

if [ condition ]; then
  # if block
else
  # else block
fi

Example:

if [ $age -ge 18 ]; then
  echo "Adult"
else
  echo "Minor"
fi

3️⃣ if-elif-else ladder

if [ condition1 ]; then
  # block1
elif [ condition2 ]; then
  # block2
else
  # else block
fi

Example:

if [ $marks -ge 90 ]; then
  echo "Grade A"
elif [ $marks -ge 75 ]; then
  echo "Grade B"
else
  echo "Grade C"
fi

4️⃣ case statement (Bash’s switch-case)

case $variable in
  pattern1) commands ;;
  pattern2) commands ;;
  *) default ;;
esac

Example:

read -p "Enter option (start/stop): " option
case $option in
  start) echo "Service starting..." ;;
  stop) echo "Service stopping..." ;;
  *) echo "Invalid option" ;;
esac

5️⃣ Short if (one-liner)

[ condition ] && echo "Yes" || echo "No"

6️⃣ Using [[ vs [

  • [[ ... ]] is safer and more versatile, especially for strings and regex.

  • Example:

if [[ $name == "Shakir" ]]; then
  echo "Hello Shakir!"
fi

🔍 Common Conditional Operators

File conditions:

  • -f file – is a regular file?

  • -d dir – is a directory?

  • -e file – exists?

  • -r, -w, -x – readable, writable, executable

🔢 Numeric Condition Operators in Bash

When making decisions based on numbers in Bash, you use specific numeric comparison operators inside test brackets like [ ] or [[ ]].


✅ Syntax Format

if [ number1 -operator number2 ]; then
  # commands
fi

🧠 Operators Breakdown

OperatorMeaningExampleDescription
-eqEqual to[ 5 -eq 5 ]True if both numbers are equal
-neNot equal to[ 5 -ne 3 ]True if numbers are not equal
-gtGreater than[ 10 -gt 3 ]True if left number is greater
-ltLess than[ 2 -lt 7 ]True if left number is less
-geGreater than or equal to[ 5 -ge 5 ]True if left is right
-leLess than or equal to[ 3 -le 5 ]True if left is right

🎯 Real Example

read -p "Enter your score: " score

if [ $score -ge 90 ]; then
  echo "Grade: A"
elif [ $score -ge 75 ]; then
  echo "Grade: B"
elif [ $score -ge 60 ]; then
  echo "Grade: C"
else
  echo "Grade: F"
fi

⚠️ Notes

  • Always put spaces around the square brackets and between the values and operators.

  • For arithmetic expressions, you can also use (( )) like:

      if (( score >= 90 )); then
        echo "Top grade!"
      fi
    

String conditions:

  • =, !=, -z (empty), -n (not empty)

🎯 Practical Use Cases

  • Automate service start/stop scripts

  • Build interactive CLI tools

  • Validate user input

  • Manage config files dynamically


✅ Summary

Decision-making in Bash gives your scripts intelligence — it lets them respond dynamically to different scenarios. Whether you're writing an installer, automation tool, or startup script — conditionals are essential.

0
Subscribe to my newsletter

Read articles from Shaharyar Shakir directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shaharyar Shakir
Shaharyar Shakir