Bird
0
0

Identify the error in this Bash script that tries to print all elements of array items:

medium📝 Debug Q14 of 15
Bash Scripting - Arrays
Identify the error in this Bash script that tries to print all elements of array items:
items=("pen" "notebook" "eraser")
for i in ${items[@]}
do
  echo $i
done
AArray declaration syntax is wrong
BMissing quotes around ${items[@]} causes word splitting
Cfor loop syntax is incorrect
Decho command is missing quotes
Step-by-Step Solution
Solution:
  1. Step 1: Check array declaration

    The array declaration is correct with parentheses and quoted elements.
  2. Step 2: Analyze the for loop iteration

    The loop uses unquoted ${items[@]}, which causes word splitting. If any element had spaces, it would split incorrectly. Quoting "${items[@]}" fixes this.
  3. Final Answer:

    Missing quotes around ${items[@]} causes word splitting -> Option B
  4. Quick Check:

    Always quote "${array[@]}" to avoid splitting [OK]
Quick Trick: Quote "${array[@]}" to prevent splitting elements [OK]
Common Mistakes:
MISTAKES
  • Not quoting array expansion in loops
  • Assuming echo needs quotes for correctness here
  • Misreading array declaration syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes