ServerAvatar Logo

Introduction to Bash For Loops: Beginner’s Guide

  • Author: Meghna Meghwani
  • Published: 18 July 2025
  • Last Updated: 17 July 2025
Introduction to Bash For Loops_ A Beginner’s Guide

Table Of Contents

If you’ve ever wished your computer could take over your repetitive tasks, like checking files, updating reports, or processing logs, then Bash for loops are about to become your new best friend.

We can say that Bash is the language that you can use to talk with your computer. And ever wondering, what is for loops? They’re like your loyal assistants who will repeat tasks as many times as you want, without getting tired.

But don’t worry if you’re not a programmer or a technical expert. We’re going to break everything down in simple terms so that anyone, even with zero scripting experience, can understand.

Ready to dive into automation and shell scripting? Let’s get started!

What is Bash and Why Use It?

Bash (short form of Bourne Again SHell) is a command-line interface used in Linux and macOS systems. You can use it to run commands, manipulate files, and automate tasks.

It’s like telling your computer what to do using text. And why should you care? Because Bash can save you hours of manual work with just a few lines of code.

What is a Loop in Programming?

Imagine you’re folding laundry. Each item goes through the same process, pick it up, fold it, and stack it. A loop in programming does the same thing: it repeats a set of actions until a condition is met.

Loops are essential for automation. They help you handle tasks like:

  • Repeating commands
  • Processing multiple files
  • Running tests on different inputs

Introduction to Bash For Loops

For loops are most commonly used type of loop in Bash. They will repeat a task for the each item that included in a list.

For Example

Bash
for item in apple banana cherry
do
  echo $item
done

The for loop in above example will print each fruit name on new line.

Output:

Bash for loops

Basic Syntax of Bash For Loops

Here’s what the structure of a Bash for loop looks like:

Bash
for variable in list
do
  command(s)
done
  • variable is a placeholder.
  • list is a set of values.
  • do starts the block of commands.
  • done ends the block.

It’s that simple!

Example of Bash For Loop: Hello, World!

Let’s write a for loop that prints “Hello, World!” five times.

Bash
for i in {1..5}
do
  echo "Hello, World!"
done

Output:

Print Hello World

What’s happening here?

  • “{1..5}” will run the loop from 1 to 5.
  • “i” takes each value from 1 to 5.
  • echo prints the message.

How Bash For Loops Work

Behind the scenes, Bash loops:

  1. It will assign a value from list to loop variable to execute.
  2. Execute the code block.
  3. Repeat until all values are used.

Think of it as like passing buckets down a line, each person (the loop variable) gets a new bucket (value) and performs the same action.

Common Use Cases of Bash For Loops

You can use Bash for loops for tasks like:

  • Renaming or moving files
  • Automating backups
  • Running scripts on multiple servers
  • Reading lines from a file
  • Monitoring system logs

Once you start using them, you’ll wonder how useful it is!

Bash For Loop with Numeric Ranges

Want to count from 1 to 10?

Bash
for i in {1..10}
do
  echo $i
done

Output:

Bash For Loop With Numeric

Or with step values:

Bash
for i in {1..10..2}
do
  echo $i
done

In the output, it will print 1, 3, 5, 7, 9 as you can see in the image below.

Output:

Bash with Step Value

Bash For Loop with Lists

You can manually define a list too:

Bash
for animal in cat dog cow
do
  echo "The animal is: $animal"
done

Output:

Bash Loop With List

Or use variables:

Bash
mylist="red blue green"
for color in $mylist
do
  echo $color
done

Output:

Bash Loop For Variable

Nested For Loops

Want to loop inside another loop? That’s called nested loop.

Bash
for i in 1 2 3
do
  for j in A B
  do
    echo "$i$j"
  done
done

This prints:
1A
1B
2A
2B
3A
3B

Output:

Nested For Loops

Control Statements in For Loops

You can control your loops with:

  • break – exits the loop early
  • continue – skips to the next iteration

Example:

Bash
for i in {1..5}
do
  if [ $i -eq 3 ]; then
    continue
  fi
  echo $i
done

This skips printing 3.

Output:

Command line

Tips for Writing Clean Bash For Loops

  • Use meaningful variable names: file, user, line are better than i or x
  • Comment your code so you remember what each part does
  • Test your loops with small inputs before running them on large data sets

Common Mistakes and How to Avoid Them

Here are some beginner pitfalls:

  • Forgetting do and done: Always include them
  • Using wrong syntax: you can use {1..10}, but not 1-10
  • Misusing spaces: Always leave spaces around in

Avoid these and your loops will run like a charm.

Conclusion 

Bash for loops are a simple yet powerful tool for anyone who wants to start automating tasks on their computer. Whether you’re a casual Linux user or a sysadmin, learning how to loop in Bash opens the door to endless possibilities. So go ahead, experiment, break things, and most importantly, automate like a pro!

Feeling overwhelmed by command-line tasks?
You’re not alone, and the good news is, you don’t have to go it alone either. If diving into Bash still feels a bit too technical, ServerAvatar offers a smarter, smoother way to manage your servers without getting buried in terminal commands.

Here’s what makes ServerAvatar a great companion:

  • Easily handle server setups, app deployments, domains, databases, and more, no deep Linux knowledge required.
  • Prefer working in the terminal? You’ll still have full SSH access and flexibility to script and automate as you like.

With ServerAvatar, whether you’re a beginner or a power user, you can manage servers your way, fast, secure, and hassle-free.

FAQs

1. What is the difference between a Bash for loop and a while loop?

for loop runs through a predefined list, while a while loop continues as long as a condition is true. Both are useful for different situations.

2. Can I use Bash for loops on Windows?

Yes, if you use Git BashWSL (Windows Subsystem for Linux), or tools like Cygwin, you can write and run Bash scripts on Windows.

3. How to loop files in a directory?

for file in *.txt
do
echo "Found file: $file"
done

This will go through every .txt file in your current directory.

4. Are Bash for loops case-sensitive?

Yes, Bash treats variables and values as case-sensitive, so “Apple” and “apple” are different.

5. Can I write multi-line commands inside a Bash for loop?

Absolutely! You can add as many commands as needed inside the do block. Just make sure each line is indented for clarity.

Stop Wasting Time on Servers. Start Building Instead.

You didn’t start your project to babysit servers. Let ServerAvatar handle deployment, monitoring, and backups — so you can focus on growth.

Deploy WordPress, Laravel, N8N, and more in minutes. No DevOps required. No command line. No stress.

🚀 Try ServerAvatar Free – No Credit Card Needed

Trusted by 10,000+ developers and growing.

Ready to Transform Your Hosting?

Join 1,000+ developers who’ve simplified server management with ServerAvatar. Get started for free and deploy your first server and application in minutes.
  • No Credit Card Required
  • Free $10 Server Credits
  • Deploy in next 10 minutes!