Bird
0
0

Identify the issue in the following Bash script snippet:

medium📝 Debug Q6 of 15
Bash Scripting - Arrays
Identify the issue in the following Bash script snippet:
arr=(red blue green yellow)
echo ${arr:2:2}
AIncorrect array slicing syntax; should use ${arr[@]:2:2}
BArray indices start at 1, so index 2 is invalid
CMissing quotes around array elements
DNo issue; the script will output 'green yellow'
Step-by-Step Solution
Solution:
  1. Step 1: Review array slicing syntax

    In Bash, to slice arrays, the syntax is ${arr[@]:start:length}. Using ${arr:start:length} is incorrect.
  2. Step 2: Identify the error

    The snippet uses ${arr:2:2} which is invalid for arrays; it is used for string slicing.
  3. Final Answer:

    Incorrect array slicing syntax; should use ${arr[@]:2:2} -> Option A
  4. Quick Check:

    Check if '@' is used for array slicing [OK]
Quick Trick: Use ${arr[@]:start:length} for array slicing [OK]
Common Mistakes:
MISTAKES
  • Using ${arr:start:length} instead of ${arr[@]:start:length}
  • Confusing string slicing with array slicing
  • Assuming array indices start at 1

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes