Bird
0
0

What is the output of this Bash script?

medium📝 Command Output Q4 of 15
Bash Scripting - Arrays
What is the output of this Bash script?
tools=("hammer" "wrench" "screwdriver")
tools+=("pliers")
unset tools[1]
echo "${tools[@]}"
Ahammer wrench screwdriver pliers
Bhammer pliers screwdriver
Chammer pliers
Dhammer screwdriver pliers
Step-by-Step Solution
Solution:
  1. Step 1: Add "pliers" to the array

    After tools+=("pliers"), array is [hammer, wrench, screwdriver, pliers].
  2. Step 2: Remove element at index 1

    unset tools[1] removes "wrench", leaving indices 0,2,3.
  3. Step 3: Print all elements

    Printing ${tools[@]} outputs existing elements in order: hammer screwdriver pliers.
  4. Final Answer:

    hammer screwdriver pliers -> Option D
  5. Quick Check:

    Unset removes element, echo prints remaining [OK]
Quick Trick: Unset removes element but does not reindex array [OK]
Common Mistakes:
MISTAKES
  • Assuming unset reindexes array
  • Expecting removed element to still print
  • Confusing order of elements after unset

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes