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) breakandcontinuegive you fine control inside loopscasesimplifies 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 :
