Bird
0
0

What is wrong with this Bash code snippet?

medium📝 Debug Q14 of 15
Bash Scripting - Arrays
What is wrong with this Bash code snippet?
my_array=(apple banana cherry)
echo ${my_array[3]}
AIndex 3 is out of range, so output is empty string
BSyntax error due to missing quotes
CArray elements must be accessed with parentheses
DIt will print 'cherry' instead of empty
Step-by-Step Solution
Solution:
  1. Step 1: Check array size and index used

    Array has 3 elements at indexes 0,1,2. Index 3 is out of range.
  2. Step 2: Understand Bash behavior for out-of-range index

    Accessing an out-of-range index returns an empty string, no error occurs.
  3. Final Answer:

    Index 3 is out of range, so output is empty string -> Option A
  4. Quick Check:

    Out-of-range index returns empty string [OK]
Quick Trick: Out-of-range indexes return empty, not errors [OK]
Common Mistakes:
MISTAKES
  • Expecting syntax error for out-of-range index
  • Using parentheses instead of brackets
  • Assuming last element is at index 3
  • Expecting output 'cherry' for index 3

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes