Recall & Review
beginner
How do you find the length of an array in Bash?
Use the syntax
${#array_name[@]} to get the number of elements in the array.Click to reveal answer
beginner
What does
${#array[@]} return in Bash?It returns the total number of elements stored in the array named
array.Click to reveal answer
beginner
Given
arr=(apple banana cherry), what is ${#arr[@]}?The output is
3 because there are three elements in the array.Click to reveal answer
intermediate
Can
${#array_name} be used to get the array length in Bash?No.
${#array_name} returns the length of the first element string, not the number of elements.Click to reveal answer
beginner
Why is knowing the array length useful in Bash scripting?
It helps to loop through all elements safely and avoid errors by knowing how many items to process.
Click to reveal answer
Which syntax correctly gets the number of elements in a Bash array named
my_array?✗ Incorrect
In Bash,
${#array[@]} returns the number of elements. ${#my_array} returns the length of the first element string.If
arr=(one two three), what does echo ${#arr[@]} print?✗ Incorrect
The command prints 3 because there are three elements in the array.
What does
${#array} return in Bash?✗ Incorrect
${#array} returns the length of the first element string, not the count of elements.Why should you use
${#array[@]} instead of ${#array[*]}?✗ Incorrect
Both count elements, but
${#array[@]} is safer in loops and expansions because it treats elements individually.What happens if you try to get the length of an empty array with
${#empty_array[@]}?✗ Incorrect
It returns 0 because there are no elements in the array.
Explain how to find the number of elements in a Bash array and why it matters.
Think about how you count items in a list before using them.
You got /3 concepts.
Describe a common mistake when trying to get array length in Bash and how to avoid it.
Focus on what ${#array} actually measures.
You got /3 concepts.