Bird
Raised Fist0
Bash Scriptingscripting~20 mins

for loop with range ({1..10}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Brace Expansion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of a simple for loop with brace expansion
What is the output of this Bash script?
for i in {1..5}; do echo $i; done
Bash Scripting
for i in {1..5}; do echo $i; done
A
1
2
3
4
5
B
0
1
2
3
4
5
C
1
2
3
4
D1 2 3 4 5
Attempts:
2 left
💡 Hint
Brace expansion generates a sequence of numbers separated by spaces.
💻 Command Output
intermediate
1:30remaining
Behavior of for loop with variable expansion inside range
What will this script output?
start=3
end=7
for i in {$start..$end}; do echo $i; done
Bash Scripting
start=3
end=7
for i in {$start..$end}; do echo $i; done
A
3
4
5
6
7
B{3..7}
C3 4 5 6 7
DSyntax error
Attempts:
2 left
💡 Hint
Brace expansion happens before variable expansion in Bash.
📝 Syntax
advanced
2:00remaining
Correct syntax for a for loop counting down with brace expansion
Which option correctly counts down from 10 to 1 using brace expansion in Bash?
Afor i in {1..10..-1}; do echo $i; done
Bfor i in {10..1}; do echo $i; done
Cfor i in {10..1..1}; do echo $i; done
Dfor i in {10..1..-1}; do echo $i; done
Attempts:
2 left
💡 Hint
Brace expansion supports a step value for counting down.
🚀 Application
advanced
2:00remaining
Using a for loop with brace expansion to create files
You want to create 5 empty files named file1.txt to file5.txt using a Bash for loop with brace expansion. Which command will do this?
Afor i in {1..5}; do echo file$i.txt; done
Btouch file{1..5}.txt
Cfor i in {1..5}; do touch file$i.txt; done
Dfor i in {1..5}; touch file$i.txt
Attempts:
2 left
💡 Hint
Use a loop to run the touch command multiple times with different file names.
🔧 Debug
expert
2:30remaining
Why does this for loop not print numbers 1 to 10?
Consider this script:
for i in {1..10} do
echo $i
done

Why does it fail to print numbers 1 to 10?
Bash Scripting
for i in {1..10} do
echo $i
done
AMissing semicolon or newline after {1..10} causes syntax error
BVariable $i is not defined before the loop
CBrace expansion does not work with for loops
DThe loop variable must be declared with 'declare' before use
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header carefully.

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

  1. Step 1: Understand the loop range syntax

    The loop uses {1..5}, which expands to numbers 1, 2, 3, 4, 5.
  2. Step 2: Check the loop body output

    Each iteration prints the current number, so numbers 1 to 5 print on separate lines.
  3. Final Answer:

    Prints numbers 1 to 5, each on a new line -> Option B
  4. 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

  1. Step 1: Identify correct brace expansion syntax

    Bash uses curly braces {start..end} for ranges, so {1..10} is correct.
  2. Step 2: Check other options for syntax errors

    Parentheses, square brackets, and angle brackets are invalid for this purpose in Bash loops.
  3. Final Answer:

    for i in {1..10}; do echo $i; done -> Option A
  4. 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

  1. Step 1: Identify loop values

    The loop variable i takes values 3, 4, 5, 6 from {3..6}.
  2. Step 2: Calculate output for each iteration

    Each iteration prints i * 2: 3*2=6, 4*2=8, 5*2=10, 6*2=12.
  3. Final Answer:

    6 8 10 12 -> Option C
  4. 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

  1. 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.
  2. Step 2: Verify other parts

    Variable $i is defined by the loop, brace syntax is correct, and loop body can be on next lines.
  3. Final Answer:

    Missing semicolon or newline after {1..5} -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    for i in {2..10..2}; do echo $i; done -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard