Chapter 57: Bash Loops

Bash Loops

Loops are one of the superpowers of scripting. Without loops, you have to repeat the same command 10 times by hand:

Bash

With loops, you write the command once and tell Bash:

“Please repeat this 100 times — or until something happens — or while this is true”

There are three main types of loops in Bash — we’ll learn all three today, step-by-step, with lots of real examples you can type right now.

1. The for loop (most common & easiest)

Use for when you know how many times you want to repeat — or when you want to go through a list of items.

Basic structure:

Bash

Example 1: Simple count from 1 to 5

Bash

Output:

text

Example 2: Better way — use {start..end}

Bash

Example 3: Loop over files (very common!)

Bash

Example 4: Loop over array

Bash

Important: Always quote “${array[@]}” — it keeps spaces inside elements safe.

Example 5: C-style for loop (classic programming style)

Bash

This is very useful when you need a counter with step:

Bash

2. The while loop (repeat while condition is true)

Use while when you want to keep going until something changes (you don’t know exactly how many times).

Structure:

Bash

Example 1: Countdown

Bash

Example 2: Ask user until correct answer

Bash

Example 3: Read file line by line (classic)

Bash

(IFS= prevents trimming spaces, -r prevents backslash interpretation)

3. The until loop (opposite of while)

Repeat until condition becomes true (rarely used, but good to know).

Bash

Almost same as while — just reversed logic.

4. Loop Control – break & continue

  • break → jump out of the loop immediately
  • continue → skip rest of this iteration, go to next one

Example:

Bash

Output:

text

5. Real-Life Script Using Loops (Type This Now!)

Create backup_files.sh:

Bash

Run:

Bash

Summary Table – Bash Loops Cheat Sheet

Loop Type Best When Syntax Example Typical Use Case
for Known number of times / list of items for i in {1..10}; do … done Process files, numbers, array items
C-style for Counter with step for ((i=0; i<10; i++)); do … done Classic counter, step 2/5/etc
while Repeat until condition false while [ $count -gt 0 ]; do … done Countdown, read file line-by-line, wait for event
until Repeat until condition true until [ $count -gt 5 ]; do … done Rare — opposite of while
break Exit loop early if [ condition ]; then break; fi Stop when found something
continue Skip this iteration if [ condition ]; then continue; fi Skip unwanted items

Got it, boss? Loops are how your script becomes tireless — it can repeat boring work 1 time or 1,000 times without complaining.

Any part confusing? Want next:

  • “Teacher, explain nested loops (loop inside loop)”
  • “How to loop over command output (find, ls, etc.)”
  • “while read line – read files properly”
  • “Infinite loops & how to stop them”
  • “select loop (simple menu)”

Just say — teacher is ready in Hyderabad! Keep writing small loops every day — even 3–4 lines — soon repeating tasks will feel effortless! 🐧🔄🔁 😄

You may also like...

Leave a Reply

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