Challenge - 5 Problems
Array Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of iterating over a bash array with spaces
What is the output of this bash script?
Bash Scripting
arr=("apple" "banana split" "cherry") for item in "${arr[@]}"; do echo "$item" done
Attempts:
2 left
💡 Hint
Think about how double quotes affect array expansion in bash loops.
✗ Incorrect
Using "${arr[@]}" with quotes preserves each array element as a single item, so 'banana split' stays together and prints on one line.
💻 Command Output
intermediate2:00remaining
Iterating over array indices in bash
What does this script output?
Bash Scripting
arr=(10 20 30) for i in "${!arr[@]}"; do echo "$i:${arr[i]}" done
Attempts:
2 left
💡 Hint
${!arr[@]} gives the indices of the array.
✗ Incorrect
The loop iterates over indices 0,1,2 and prints index:value pairs.
📝 Syntax
advanced2:00remaining
Identify the syntax error in array iteration
Which option contains a syntax error when iterating over a bash array?
Bash Scripting
arr=(a b c)
Attempts:
2 left
💡 Hint
Check how arrays are expanded in bash.
✗ Incorrect
Option C has an invalid array index ${arr[)} which causes a 'bad substitution' syntax error. The other options are syntactically valid.
💻 Command Output
advanced2:00remaining
Output when iterating over array with unquoted expansion
What is the output of this script?
Bash Scripting
arr=("one two" three) for i in ${arr[@]}; do echo "$i" done
Attempts:
2 left
💡 Hint
Unquoted expansion splits elements on spaces.
✗ Incorrect
Without quotes, each element is split by spaces, so 'one two' becomes two words.
🚀 Application
expert3:00remaining
Count total words from all array elements in bash
Given an array with elements containing multiple words, which script correctly counts the total number of words across all elements?
Bash Scripting
arr=("hello world" "foo bar baz" "single")
Attempts:
2 left
💡 Hint
Use quotes to preserve elements and count words inside each element.
✗ Incorrect
Option A correctly iterates over each element as a whole string and counts words inside it, summing them up.