📖 Scenario: Imagine you want to count from 1 to 10 using a simple script. This is like counting steps as you walk up a staircase.
🎯 Goal: You will write a Bash script that uses a for loop with a range {1..10} to print numbers from 1 to 10, one number per line.
📋 What You'll Learn
Create a Bash script variable to hold a greeting message.
Create a variable to hold the range of numbers from 1 to 10.
Use a for loop with the range {1..10} to print each number.
Print the greeting message before the numbers.
💡 Why This Matters
🌍 Real World
Counting or looping through numbers is common in scripts that automate tasks like backups, monitoring, or batch processing files.
💼 Career
Knowing how to use loops and ranges in Bash is a basic skill for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create a greeting message variable
Create a Bash variable called greeting and set it to the exact text "Counting from 1 to 10:".
Bash Scripting
Hint
Use variable_name="text" to create a string variable in Bash.
2
Create a variable for the number range
Create a Bash variable called numbers and set it to the range {1..10} exactly as numbers="{1..10}".
Bash Scripting
Hint
Remember to put the range in quotes to assign it as a string.
3
Use a for loop with the range to print numbers
Write a for loop using for i in {1..10} to print each number i on its own line using echo.
Bash Scripting
Hint
The loop starts with for i in {1..10}, then do, then the commands, and ends with done.
4
Print the greeting and the numbers
Add a line to print the greeting variable before the loop using echo "$greeting".
Bash Scripting
Hint
Use echo "$greeting" before the loop to show the message.
Practice
(1/5)
1. What does the following Bash loop do? for i in {1..5}; do echo $i; done
easy
A. Causes a syntax error
B. Prints numbers 1 to 5, each on a new line
C. Prints numbers 0 to 4, each on a new line
D. Prints the string '{1..5}' once
Solution
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 B
Quick Check:
Range {1..5} loops from 1 to 5 [OK]
Hint: Curly braces {1..5} generate numbers 1 to 5 [OK]
Common Mistakes:
Thinking {1..5} is a string, not a range
Confusing range start and end values
Expecting zero-based counting
2. Which of the following is the correct syntax to loop from 1 to 10 in Bash using brace expansion?
easy
A. for i in {1..10}; do echo $i; done
B. for i in [1..10]; do echo $i; done
C. for i in (1..10); do echo $i; done
D. for i in <1..10>; do echo $i; done
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 A
Quick Check:
Brace expansion uses curly braces {..} [OK]
Hint: Use curly braces {start..end} for ranges in Bash loops [OK]
Common Mistakes:
Using parentheses or brackets instead of braces
Missing the 'in' keyword
Forgetting to close the loop with 'done'
3. What is the output of this script?
for i in {3..6}; do echo $((i * 2)); done
medium
A. 2 4 6 8
B. 3 4 5 6
C. 6
8
10
12
D. Error: invalid arithmetic
Solution
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 C
Quick Check:
Multiply each i by 2 [OK]
Hint: Multiply loop variable inside $(( )) for arithmetic [OK]
Common Mistakes:
Confusing loop values with output
Using single parentheses instead of $(( ))
Expecting space-separated output on one line
4. Find the error in this Bash loop:
for i in {1..5} do echo $i done
medium
A. Variable $i is not defined
B. Loop body must be on the same line as for
C. Brace expansion syntax is wrong
D. Missing semicolon or newline after {1..5}
Solution
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 D
Quick Check:
Loop header must end properly before 'do' [OK]
Hint: Put semicolon or newline before 'do' in for loops [OK]
Common Mistakes:
Omitting semicolon or newline before 'do'
Misunderstanding brace expansion syntax
Thinking loop body must be on same line
5. You want to print only even numbers from 2 to 10 using a Bash for loop with brace expansion. Which script achieves this?
hard
A. for i in {2..10..2}; do echo $i; done
B. for i in {1..10}; do if [ $i % 2 == 0 ]; then echo $i; fi; done
C. for i in {2..10}; do echo $((i*2)); done
D. for i in {2..10..3}; do echo $i; done
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 A
Quick Check:
Use {start..end..step} for stepped ranges [OK]
Hint: Use {start..end..step} to loop with steps in Bash [OK]
Common Mistakes:
Not using step in brace expansion
Multiplying loop variable instead of stepping
Using wrong step size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.