Bird
0
0

You have an array files=(img1.jpg img2.jpg img3.jpg doc1.pdf doc2.pdf). How do you slice only the image files using array slicing?

hard🚀 Application Q8 of 15
Bash Scripting - Arrays
You have an array files=(img1.jpg img2.jpg img3.jpg doc1.pdf doc2.pdf). How do you slice only the image files using array slicing?
A${files[@]:3:2}
B${files[@]:0:3}
C${files[@]:1:3}
D${files[@]:2:2}
Step-by-Step Solution
Solution:
  1. Step 1: Identify image files positions

    Image files are first 3 elements at indexes 0,1,2.
  2. Step 2: Slice first 3 elements

    Use ${files[@]:0:3} to get 'img1.jpg img2.jpg img3.jpg'.
  3. Final Answer:

    ${files[@]:0:3} -> Option B
  4. Quick Check:

    Slice first 3 elements for images [OK]
Quick Trick: Slice from 0 length 3 to get first three elements [OK]
Common Mistakes:
MISTAKES
  • Starting slice from wrong index
  • Taking wrong length
  • Mixing image and document files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes