Bird
0
0

In Bash scripting, how do you access the first element of an array named fruits?

easy🧠 Conceptual Q11 of 15
Bash Scripting - Arrays
In Bash scripting, how do you access the first element of an array named fruits?
AUsing <code>${fruits[1]}</code>
BUsing <code>fruits[0]</code>
CUsing <code>$fruits[0]</code>
DUsing <code>${fruits[0]}</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing in Bash

    Array indexes start at 0, so the first element is at index 0.
  2. Step 2: Use correct syntax to access element

    In Bash, array elements are accessed with ${array_name[index]}. So, ${fruits[0]} gets the first element.
  3. Final Answer:

    Using ${fruits[0]} -> Option D
  4. Quick Check:

    First element = ${fruits[0]} [OK]
Quick Trick: Remember arrays start at 0, use ${array[0]} for first element [OK]
Common Mistakes:
MISTAKES
  • Using index 1 for first element
  • Omitting ${} around array access
  • Using $fruits[0] without braces
  • Treating array like a string variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes