Bird
0
0

Which command removes the last element from a Bash array named colors?

easy🧠 Conceptual Q2 of 15
Bash Scripting - Arrays
Which command removes the last element from a Bash array named colors?
Acolors+=()
Bcolors=(${colors[@]:1:${#colors[@]}-1})
Cunset colors[${#colors[@]}-1]
Dunset colors[0]
Step-by-Step Solution
Solution:
  1. Step 1: Identify last element index

    The last element index is length minus one: ${#colors[@]}-1.
  2. Step 2: Use unset to remove that element

    unset colors[${#colors[@]}-1] deletes the last element from the array.
  3. Final Answer:

    unset colors[${#colors[@]}-1] -> Option C
  4. Quick Check:

    Remove last element = A [OK]
Quick Trick: Use unset with last index to remove last element [OK]
Common Mistakes:
MISTAKES
  • Using unset colors[0] removes first element, not last
  • Appending empty element instead of removing
  • Trying to slice array without reassignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes