0
0
Bash Scriptingscripting~3 mins

Why for loop with range ({1..10}) in Bash Scripting? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could tell your computer to do repetitive tasks for you with just one simple command?

The Scenario

Imagine you need to print numbers from 1 to 10 one by one in your terminal. Doing this manually means typing each number and command separately, which is tiring and boring.

The Problem

Typing commands for each number wastes time and can cause mistakes like skipping numbers or typing wrong commands. It's slow and not fun, especially if you want to do it many times.

The Solution

The for loop with range ({1..10}) lets you write a simple command that automatically repeats actions for numbers 1 through 10. It saves time and avoids errors by doing the counting for you.

Before vs After
Before
echo 1
echo 2
echo 3
...
echo 10
After
for i in {1..10}; do echo $i; done
What It Enables

This lets you quickly repeat tasks many times without extra typing, making your work faster and less error-prone.

Real Life Example

Say you want to create 10 folders named folder1 to folder10. Instead of making each folder one by one, a for loop does it all in seconds.

Key Takeaways

Manual repetition is slow and error-prone.

For loop with range automates repeated actions easily.

It saves time and reduces mistakes in scripts.