Bird
0
0

In bash scripting, which expression correctly gives the number of elements in an array named my_array?

easy🧠 Conceptual Q11 of 15
Bash Scripting - Arrays
In bash scripting, which expression correctly gives the number of elements in an array named my_array?
A${#my_array[@]}
B${my_array[@]}
C${#my_array}
D${my_array}
Step-by-Step Solution
Solution:
  1. Step 1: Understand array length syntax

    In bash, ${#array[@]} returns the count of elements in the array.
  2. Step 2: Differentiate from other forms

    ${#array} returns the length of the first element, not the number of elements.
  3. Final Answer:

    ${#my_array[@]} -> Option A
  4. Quick Check:

    Array length = ${#my_array[@]} [OK]
Quick Trick: Use ${#array[@]} to get array size, not ${#array} [OK]
Common Mistakes:
MISTAKES
  • Using ${#array} which gives length of first element
  • Using ${array[@]} which expands elements, not count
  • Confusing ${array} with array length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes