π Bash Control Flow β if/else, Loops, Case, and Arithmetic in Shell Scripts
π§² Introduction β Direct the Flow of Logic in Your Bash Scripts
In Bash scripting, control flow allows you to make decisions, repeat actions, and branch your logic using constructs like if, for, while, case, and arithmetic operations. These tools form the backbone of dynamic scriptingβenabling automation, condition checks, and user input handling.
π― In this guide, youβll learn:
- How to use if,elif,else, and condition tests
- Looping with for,while, anduntil
- Using breakandcontinueto manage loop flow
- Handling multi-choice logic using casestatements
- Working with numeric expressions and comparisons
π Topics Covered
| πΉ Topic | π Description | 
|---|---|
| π’ Bash: Decision Making ( if,[[ ]],else) | Conditional branching using logical tests | 
| π’ Bash: Looping Constructs ( for,while) | Repeat blocks using count, conditions, or lists | 
| π’ Bash: Loop Control ( break,continue) | Interrupt or skip parts of a loop | 
| π’ Bash: Case Statements ( case,in,esac) | Multi-branch logic for pattern matching | 
| π’ Bash: Operators ( -eq,-gt,==, etc.) | Logical, comparison, and file operators | 
| π’ Bash: Arithmetic ( let,(( )),expr) | Perform integer calculations inside scripts | 
π’ Bash: Decision Making (if, elif, else, [[ ]])
πΉ Syntax:
if [[ $num -gt 10 ]]; then
  echo "Greater than 10"
elif [[ $num -eq 10 ]]; then
  echo "Equals 10"
else
  echo "Less than 10"
fi
β
 Use [[ ]] for safe comparisons and strings. Always quote variables inside.
π’ Bash: Looping Constructs (for, while, until)
πΉ For Loop:
for i in 1 2 3; do
  echo "Number $i"
done
πΉ While Loop:
count=1
while [[ $count -le 3 ]]; do
  echo "Count is $count"
  ((count++))
done
πΉ Until Loop:
n=1
until [[ $n -gt 3 ]]; do
  echo "n is $n"
  ((n++))
done
β
 while runs while true; until runs until condition is true.
π’ Bash: Loop Control (break, continue)
πΉ Break Example:
for i in {1..5}; do
  if [[ $i -eq 3 ]]; then
    break
  fi
  echo $i
done
πΉ Continue Example:
for i in {1..5}; do
  if [[ $i -eq 3 ]]; then
    continue
  fi
  echo $i
done
β
 Use break to exit a loop, continue to skip to the next iteration.
π’ Bash: Case Statements (case, in, esac)
πΉ Syntax:
read -p "Enter a fruit: " fruit
case $fruit in
  apple) echo "Red fruit";;
  banana) echo "Yellow fruit";;
  *) echo "Unknown fruit";;
esac
β
 case is cleaner than nested if when checking multiple values.
π’ Bash: Operators (-eq, -gt, ==, !=, -f, etc.)
πΉ Integer Comparison:
| Operator | Meaning | 
|---|---|
| -eq | Equal to | 
| -ne | Not equal to | 
| -gt | Greater than | 
| -lt | Less than | 
| -ge | Greater or equal | 
| -le | Less or equal | 
πΉ String Comparison:
if [[ "$a" == "$b" ]]; then
πΉ File Test Operators:
| Operator | Checks for | 
|---|---|
| -f | Regular file | 
| -d | Directory | 
| -e | Exists | 
| -r | Read permission | 
| -x | Executable | 
π’ Bash: Arithmetic (let, (( )), expr)
πΉ let Command:
let result=5+3
echo $result
πΉ (( )) Syntax (preferred):
a=5
b=3
((sum = a + b))
echo $sum
πΉ expr (legacy, use spaces):
expr 5 + 3
β
 Use (( )) for modern integer math with support for conditions too.
π Summary β Bash Control Flow
Control flow makes Bash scripts dynamic and powerful. You can branch, repeat, break, and calculateβenabling automation logic thatβs clean and scalable.
π Key Takeaways:
- Use if,elif,elsefor decision making with[[ ]]
- Loops allow repeated execution (for,while,until)
- breakand- continuegive you fine control inside loops
- casesimplifies multi-option conditionals
- Use arithmetic operators and (( ))for math
βοΈ Real-World Applications:
- User menu-driven shell tools
- Server monitoring & scheduled job logic
- Data processing loops and error handling
β Frequently Asked Questions: Bash Control Flow
β Can I use floating point math in Bash?
β
 Not directly. Use bc or switch to a language like Python for float support.
β Whatβs the difference between [] and [[ ]] in Bash?
β
 [[ ]] is more powerfulβsupports regex, string handling, and avoids quoting issues.
β Is let still recommended?
β
 It works, but (( )) is preferred for clarity and compatibility.
Share Now :
