Bash Tutorial
Estimated reading: 3 minutes 41 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 :

Leave a Reply

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

Share

πŸ”„ 3. Bash Control Flow

Or Copy Link

CONTENTS
Scroll to Top