Bash Tutorial
Estimated reading: 3 minutes 420 views

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, and until
  • Using break and continue to manage loop flow
  • Handling multi-choice logic using case statements
  • 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:

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

String Comparison:

if [[ "$a" == "$b" ]]; then

File Test Operators:

OperatorChecks for
-fRegular file
-dDirectory
-eExists
-rRead permission
-xExecutable

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, else for decision making with [[ ]]
  • Loops allow repeated execution (for, while, until)
  • break and continue give you fine control inside loops
  • case simplifies 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 :
Share

πŸ”„ 3. Bash Control Flow

Or Copy Link

CONTENTS
Scroll to Top