Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a simple for loop
What is the output of this bash script?
Bash Scripting
for i in 1 2 3; do echo "Number $i"; done
Attempts:
2 left
💡 Hint
Look at how the echo command prints each item on a new line.
✗ Incorrect
The for loop iterates over the list 1 2 3, printing 'Number' followed by each number on its own line.
💻 Command Output
intermediate2:00remaining
While loop counting down
What will this bash script output?
Bash Scripting
count=3 while [ $count -gt 0 ]; do echo "Counting $count" count=$((count - 1)) done
Attempts:
2 left
💡 Hint
The loop runs while count is greater than zero.
✗ Incorrect
The while loop prints the current count and decreases it until it reaches zero, then stops.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this for loop
Which option shows the correct syntax for a bash for loop that prints numbers 1 to 3?
Bash Scripting
for i in 1 2 3 do echo $i done
Attempts:
2 left
💡 Hint
Remember the semicolon before 'do' is required.
✗ Incorrect
Option A uses the correct syntax with 'in' keyword, semicolon before 'do', and proper loop body.
🔧 Debug
advanced2:00remaining
Why does this while loop run infinitely?
What is the reason this bash script runs forever?
Bash Scripting
count=1 while [ $count -le 3 ] do echo $count done
Attempts:
2 left
💡 Hint
Check if the loop changes the variable controlling the condition.
✗ Incorrect
The loop never changes 'count', so the condition is always true, causing an infinite loop.
🚀 Application
expert3:00remaining
Create a loop to list all .txt files with line counts
Which bash script correctly lists all '.txt' files in the current folder and prints their line counts?
Attempts:
2 left
💡 Hint
Use command substitution and redirect wc input to avoid extra filename in output.
✗ Incorrect
Option C uses a for loop with command substitution and input redirection to print only line counts with filenames.