0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.