What if you could tell your computer to do repetitive tasks for you with just one simple command?
Why for loop with range ({1..10}) in Bash Scripting? - Purpose & Use Cases
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.
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 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.
echo 1 echo 2 echo 3 ... echo 10
for i in {1..10}; do echo $i; done
This lets you quickly repeat tasks many times without extra typing, making your work faster and less error-prone.
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.
Manual repetition is slow and error-prone.
For loop with range automates repeated actions easily.
It saves time and reduces mistakes in scripts.
Practice
for i in {1..5}; do echo $i; doneSolution
Step 1: Understand the loop range syntax
The loop uses {1..5}, which expands to numbers 1, 2, 3, 4, 5.Step 2: Check the loop body output
Each iteration prints the current number, so numbers 1 to 5 print on separate lines.Final Answer:
Prints numbers 1 to 5, each on a new line -> Option BQuick Check:
Range {1..5} loops from 1 to 5 [OK]
- Thinking {1..5} is a string, not a range
- Confusing range start and end values
- Expecting zero-based counting
Solution
Step 1: Identify correct brace expansion syntax
Bash uses curly braces {start..end} for ranges, so {1..10} is correct.Step 2: Check other options for syntax errors
Parentheses, square brackets, and angle brackets are invalid for this purpose in Bash loops.Final Answer:
for i in {1..10}; do echo $i; done -> Option AQuick Check:
Brace expansion uses curly braces {..} [OK]
- Using parentheses or brackets instead of braces
- Missing the 'in' keyword
- Forgetting to close the loop with 'done'
for i in {3..6}; do echo $((i * 2)); doneSolution
Step 1: Identify loop values
The loop variable i takes values 3, 4, 5, 6 from {3..6}.Step 2: Calculate output for each iteration
Each iteration prints i * 2: 3*2=6, 4*2=8, 5*2=10, 6*2=12.Final Answer:
6 8 10 12 -> Option CQuick Check:
Multiply each i by 2 [OK]
- Confusing loop values with output
- Using single parentheses instead of $(( ))
- Expecting space-separated output on one line
for i in {1..5} do
echo $i
doneSolution
Step 1: Check for syntax correctness in loop header
The for loop header must end with a semicolon or newline before 'do'. Here, 'do' follows directly after {1..5} without either, causing a syntax error.Step 2: Verify other parts
Variable $i is defined by the loop, brace syntax is correct, and loop body can be on next lines.Final Answer:
Missing semicolon or newline after {1..5} -> Option DQuick Check:
Loop header must end properly before 'do' [OK]
- Omitting semicolon or newline before 'do'
- Misunderstanding brace expansion syntax
- Thinking loop body must be on same line
Solution
Step 1: Understand brace expansion with step
Brace expansion supports {start..end..step}, so {2..10..2} generates even numbers 2,4,6,8,10.Step 2: Check other options
B uses [ ] test which performs string comparison, not arithmetic modulo, so never prints evens; C multiplies numbers incorrectly to 4,6,8,10,12,14,16,18,20; D steps by 3, not 2.Final Answer:
for i in {2..10..2}; do echo $i; done -> Option AQuick Check:
Use {start..end..step} for stepped ranges [OK]
- Not using step in brace expansion
- Multiplying loop variable instead of stepping
- Using wrong step size
Ready to prove it?
Pass the test and this pattern moves unknown
