Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
Output of a simple list-based for loop
What is the output of this Bash script?
Bash Scripting
for item in apple banana cherry; do echo $item done
Attempts:
2 left
💡 Hint
Remember that the loop runs once for each item in the list and echo prints each item on a new line.
✗ Incorrect
The for loop iterates over each word in the list and echoes it. Each echo outputs on a new line.
💻 Command Output
intermediate1:30remaining
Counting items in a list with a for loop
What is the value of the variable count after running this script?
Bash Scripting
count=0 for word in one two three four; do count=$((count + 1)) done echo $count
Attempts:
2 left
💡 Hint
The loop increments count once for each word in the list.
✗ Incorrect
The loop runs 4 times, increasing count by 1 each time, so count ends at 4.
💻 Command Output
advanced2:00remaining
Output of nested for loops with lists
What is the output of this Bash script?
Bash Scripting
for i in 1 2; do for j in a b; do echo "$i$j" done done
Attempts:
2 left
💡 Hint
The outer loop runs twice, and for each iteration the inner loop runs twice.
✗ Incorrect
The script prints each combination of i and j on its own line: 1a, 1b, 2a, 2b.
💻 Command Output
advanced2:00remaining
Effect of quotes in list-based for loop
What is the output of this Bash script?
Bash Scripting
for item in "one two" three; do echo "$item" done
Attempts:
2 left
💡 Hint
Quotes group words together as a single item in the list.
✗ Incorrect
The first item is the string 'one two' as one item, then 'three' as the second item.
💻 Command Output
expert2:30remaining
Output of for loop with command substitution and word splitting
What is the output of this Bash script?
Bash Scripting
for file in $(echo "file1.txt file2.txt"); do echo "$file" done
Attempts:
2 left
💡 Hint
Command substitution outputs a string that is split into words for the loop.
✗ Incorrect
The command substitution outputs two words separated by space, so the loop runs twice.