๐ 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, andelse
- 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
- thenmust 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
| Operator | Meaning | 
|---|---|
| -eq | Equal to | 
| -ne | Not equal to | 
| -gt | Greater than | 
| -lt | Less than | 
| -ge | Greater or equal | 
| -le | Less or equal | 
๐งช Example:
num=20
if [ $num -gt 10 ]; then
  echo "Number is greater than 10"
fi
๐ String Comparisons
| Operator | Meaning | 
|---|---|
| ==or= | Equal | 
| != | Not equal | 
| -z | String is empty | 
| -n | String is not empty | 
๐งช Example:
text="bash"
if [[ -n "$text" ]]; then
  echo "Text is not empty"
fi
๐ File Test Conditions
| Condition | Checks For | 
|---|---|
| -f file | Regular file exists | 
| -d directory | Directory exists | 
| -e file | File or directory exists | 
| -r file | Read permission | 
| -w file | Write permission | 
| -x file | Execute permission | 
๐งช Example:
if [[ -f "script.sh" ]]; then
  echo "File exists"
else
  echo "File does not exist"
fi
๐ Multiple Conditions โ Logical Operators
| Operator | Description | 
|---|---|
| && | 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, andelseto handle conditions
- Use [[ ]]for safer and enhanced conditions
- Use -eq,-gt,==,!=etc. for numeric/string comparisons
- File test operators like -for-dare 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 :
