Bird
0
0

What will be the output of this Bash script?

medium📝 Command Output Q5 of 15
Bash Scripting - Arrays
What will be the output of this Bash script?
numbers=(1 2 3 4 5)
numbers=(${numbers[@]:0:3})
echo "${numbers[@]}"
A1 2 3 4 5
B1 2 3
C3 4 5
D2 3 4
Step-by-Step Solution
Solution:
  1. Step 1: Slice first 3 elements

    ${numbers[@]:0:3} extracts elements from index 0 to 2: 1 2 3.
  2. Step 2: Reassign sliced array

    Assigning back to numbers keeps only those 3 elements.
  3. Step 3: Print the array

    echo "${numbers[@]}" outputs: 1 2 3.
  4. Final Answer:

    1 2 3 -> Option B
  5. Quick Check:

    Array slicing keeps first 3 elements [OK]
Quick Trick: Use slicing syntax to keep part of array [OK]
Common Mistakes:
MISTAKES
  • Confusing slice start and length
  • Expecting original array unchanged
  • Using wrong indices for slicing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes