Bird
0
0

What is the output of this Bash script?

medium📝 Command Output Q13 of 15
Bash Scripting - Arrays
What is the output of this Bash script?
items=("pen" "pencil" "eraser")
items+=("marker")
unset 'items[1]'
for item in "${items[@]}"; do
  echo "$item"
done
Apen marker
Bpen pencil eraser marker
Cpen eraser marker
Dpen pencil marker
Step-by-Step Solution
Solution:
  1. Step 1: Analyze array modifications

    Initial array: ["pen", "pencil", "eraser"]. Then add "marker" at the end: ["pen", "pencil", "eraser", "marker"]. Then remove element at index 1 ("pencil").
  2. Step 2: Determine final array elements

    After removal, array elements are: index 0="pen", index 2="eraser", index 3="marker". Looping over ${items[@]} skips unset indices, so output is "pen", "eraser", "marker" each on a new line.
  3. Final Answer:

    pen eraser marker -> Option C
  4. Quick Check:

    Remove index 1 and add marker = pen, eraser, marker [OK]
Quick Trick: Unset removes element but keeps others; loop skips unset [OK]
Common Mistakes:
MISTAKES
  • Assuming unset shifts elements left
  • Expecting pencil to still print
  • Not realizing loop skips unset indices

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes