๐Ÿ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 47 views

๐Ÿš Bash Decision Making โ€“ if, elif, else, and [[ ... ]] Conditions Explained


๐Ÿงฒ Introduction to Bash Conditional Statements โ€“ if, elif, else, and [[ ]]

Decision-making is a core part of Bash scripting. The ability to make choices using if, elif, and else statements allows scripts to behave dynamically based on input, file checks, string comparisons, or numeric conditions.

Bash also uses [ ], [[ ]], and (( )) syntaxes to evaluate conditions. Understanding these structures is essential for writing logic-based scripts.


๐ŸŽฏ In this article, youโ€™ll learn:

  • How to write conditional statements using if, elif, and else
  • The difference between [ ] and [[ ]] in Bash
  • Numeric and string comparisons
  • Real-world decision-making examples

๐Ÿ” Basic Syntax: if, elif, else

if condition; then
  # code block
elif another_condition; then
  # alternative block
else
  # fallback block
fi
  • then must follow if (can be on a new line or same line with ;)
  • Each block ends with fi (if spelled backwards)

๐Ÿงช Example: Basic String Check

name="Vaibhav"

if [[ $name == "Vaibhav" ]]; then
  echo "Name matched!"
else
  echo "Name does not match."
fi

โœ… Output:

Name matched!

๐Ÿ”ข Numeric Comparisons with -eq, -gt, -lt

OperatorMeaning
-eqEqual to
-neNot equal to
-gtGreater than
-ltLess than
-geGreater or equal
-leLess or equal

๐Ÿงช Example:

num=20

if [ $num -gt 10 ]; then
  echo "Number is greater than 10"
fi

๐Ÿ”  String Comparisons

OperatorMeaning
== or =Equal
!=Not equal
-zString is empty
-nString is not empty

๐Ÿงช Example:

text="bash"

if [[ -n "$text" ]]; then
  echo "Text is not empty"
fi

๐Ÿ“ File Test Conditions

ConditionChecks For
-f fileRegular file exists
-d directoryDirectory exists
-e fileFile or directory exists
-r fileRead permission
-w fileWrite permission
-x fileExecute permission

๐Ÿงช Example:

if [[ -f "script.sh" ]]; then
  echo "File exists"
else
  echo "File does not exist"
fi

๐Ÿ” Multiple Conditions โ€“ Logical Operators

OperatorDescription
&&Logical AND
`
!Logical NOT

๐Ÿงช Example:

if [[ $age -gt 18 && $age -lt 60 ]]; then
  echo "Adult within working age"
fi

๐Ÿ” Nested if Conditions

if [[ $user == "admin" ]]; then
  if [[ $status == "active" ]]; then
    echo "Admin is active"
  fi
fi

๐Ÿง  Nested conditions are useful when checking multiple levels of logic.


๐Ÿงฑ [ ] vs [[ ]] โ€“ What’s the Difference?

Feature[ ][[ ]]
POSIX-compliantโœ… YesโŒ No
Safer with variablesโŒ Limitedโœ… Yes
Supports pattern matchingโŒ Noโœ… Yes
Better error handlingโŒ Noโœ… Yes

โœ… Use [[ ]] when writing modern, safe Bash scripts. Use [ ] for POSIX-compatible minimal scripts.


๐Ÿ“Œ Summary โ€“ Bash Conditional Statements

Conditional logic is a building block of intelligent Bash scripts. Using if, elif, else, and [[ ... ]] allows your scripts to make decisions, validate inputs, and perform context-based operations.

๐Ÿ” Key Takeaways:

  • Use if, elif, and else to handle conditions
  • Use [[ ]] for safer and enhanced conditions
  • Use -eq, -gt, ==, != etc. for numeric/string comparisons
  • File test operators like -f or -d are great for validations

โš™๏ธ Real-world Uses:

  • Check if files exist before modifying
  • Validate user input or arguments
  • Branch script logic based on time, user, or status

โ“ FAQ โ€“ Bash Conditional Statements


โ“ What is the difference between if [ ] and if [[ ]]?
โœ… [ ] is POSIX-compliant but limited. [[ ]] is a Bash enhancement with safer syntax and better string matching.


โ“ Can I use == inside [ ]?
โœ… Yes, but it’s better to use = inside [ ] and reserve == for [[ ]].


โ“ How do I check if a string is empty in Bash?
โœ… Use:

if [[ -z "$var" ]]; then
  echo "Empty"
fi

โ“ How do I compare numbers in Bash?
โœ… Use -eq, -ne, -lt, -gt, etc.:

if [[ $a -gt 5 ]]; then echo "Greater"; fi

โ“ How do I combine two conditions in Bash?
โœ… Use && for AND, || for OR:

if [[ $a -gt 0 && $b -lt 100 ]]; then
  echo "Within range"
fi

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Decision Making (if, elif, else, [[ ]])

Or Copy Link

CONTENTS
Scroll to Top