Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Array length in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array length
Define array
↓
Use length syntax
↓
Shell calculates length
↓
Output length
The shell script defines an array, then uses special syntax to get its length, which is printed as output.
Execution Sample
Bash Scripting
my_array=(apple banana cherry)
echo ${#my_array[@]}
This script defines an array with three fruits and prints the number of elements in it.
Execution Table
StepActionVariable/ExpressionValue/ResultOutput
1Define arraymy_array(apple banana cherry)
2Evaluate length expression${#my_array[@]}3
3Print lengthecho ${#my_array[@]}33
💡 All steps complete, array length 3 printed.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
my_arrayundefined(apple banana cherry)(apple banana cherry)(apple banana cherry)
${#my_array[@]}undefinedundefined33
Key Moments - 2 Insights
Why do we use ${#my_array[@]} instead of just ${#my_array}?
Using ${#my_array[@]} counts all elements in the array (see execution_table step 2). ${#my_array} would give the length of the first element string, not the array size.
What happens if the array is empty?
If the array is empty, ${#my_array[@]} evaluates to 0, meaning zero elements (not shown here but same syntax).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of ${#my_array[@]} at step 2?
A3
Bapple
Cundefined
D0
💡 Hint
Check the 'Value/Result' column in execution_table row for step 2.
At which step is the array actually printed to the screen?
AStep 1
BStep 3
CStep 2
DNo output is printed
💡 Hint
Look at the 'Output' column in execution_table to find when output appears.
If we add one more fruit to the array, how would the output change?
AOutput would stay 3
BOutput would be the new fruit name
COutput would be 4
DOutput would be empty
💡 Hint
Refer to variable_tracker and how length counts elements in the array.
Concept Snapshot
Array length in bash:
- Define array: my_array=(item1 item2 ...)
- Get length: ${#my_array[@]}
- Prints number of elements
- Use [@] to count all elements
- Empty array length is 0
Full Transcript
This example shows how to find the length of an array in bash scripting. First, an array named my_array is defined with three elements: apple, banana, and cherry. Then, the expression ${#my_array[@]} is used to get the number of elements in the array. The shell calculates this length as 3. Finally, the echo command prints the number 3 to the screen. It is important to use ${#my_array[@]} to count all elements, not just ${#my_array}, which would give the length of the first element string. If the array were empty, the length would be 0. This simple script helps count how many items are stored in a bash array.

Practice

(1/5)
1. In bash scripting, which expression correctly gives the number of elements in an array named my_array?
easy
A. ${#my_array[@]}
B. ${my_array[@]}
C. ${#my_array}
D. ${my_array}

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]
Hint: Use ${#array[@]} to get array size, not ${#array} [OK]
Common Mistakes:
  • Using ${#array} which gives length of first element
  • Using ${array[@]} which expands elements, not count
  • Confusing ${array} with array length
2. Which of the following is the correct syntax to store the length of a bash array files into a variable count?
easy
A. count=${files[@]}
B. count=${#files}
C. count=${#files[@]}
D. count=${files}

Solution

  1. Step 1: Use correct length syntax

    To get the number of elements, use ${#files[@]}.
  2. Step 2: Assign to variable

    Assigning with count=${#files[@]} stores the array length in count.
  3. Final Answer:

    count=${#files[@]} -> Option C
  4. Quick Check:

    Variable count = ${#files[@]} [OK]
Hint: Assign length with count=${#array[@]} to get element count [OK]
Common Mistakes:
  • Using count=${files[@]} which assigns all elements, not length
  • Using count=${#files} which gives length of first element
  • Forgetting to use ${} around the expression
3. What will be the output of this bash script?
arr=(apple banana cherry)
echo ${#arr[@]}
medium
A. apple
B. 18
C. 0
D. 3

Solution

  1. Step 1: Understand array initialization

    The array arr has 3 elements: apple, banana, cherry.
  2. Step 2: Use ${#arr[@]} to get count

    ${#arr[@]} returns the number of elements, which is 3.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Array length = 3 [OK]
Hint: Count elements with ${#array[@]} to get number, not string length [OK]
Common Mistakes:
  • Confusing ${#arr[@]} with ${#arr} which gives length of first element
  • Expecting total string length instead of element count
  • Printing array without expansion
4. Identify the error in this bash script snippet that tries to print the number of elements in array data:
data=(1 2 3 4)
echo ${#data}
medium
A. It prints the length of the first element, not the array size
B. The array is not declared properly
C. The echo command is missing quotes
D. The syntax ${#data} is invalid in bash

Solution

  1. Step 1: Check array length syntax

    ${#data} returns the length of the first element (string length), not the number of elements.
  2. Step 2: Correct syntax for array length

    Use ${#data[@]} to get the count of elements in the array.
  3. Final Answer:

    It prints the length of the first element, not the array size -> Option A
  4. Quick Check:

    Use ${#array[@]} for count, not ${#array} [OK]
Hint: Use ${#array[@]} to get count, ${#array} gives first element length [OK]
Common Mistakes:
  • Assuming ${#array} gives number of elements
  • Not using [@] inside the braces
  • Thinking echo needs quotes for this syntax
5. You have a bash array items that may be empty or have elements. You want to print "Empty" if it has zero elements, or "Count: N" where N is the number of elements. Which script snippet correctly does this?
hard
A. if [ ${#items} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi
B. if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi
C. if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items}"; fi
D. if [ ${#items} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items}"; fi

Solution

  1. Step 1: Check condition for empty array

    Use ${#items[@]} to get the number of elements. If zero, array is empty.
  2. Step 2: Print count correctly

    Use ${#items[@]} to print the count of elements, not ${#items} which is length of first element.
  3. Final Answer:

    if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi -> Option B
  4. Quick Check:

    Use ${#array[@]} for count in both condition and output [OK]
Hint: Always use ${#array[@]} to check and print array length [OK]
Common Mistakes:
  • Using ${#array} instead of ${#array[@]} for count
  • Mixing length of first element with element count
  • Forgetting to use [ ] in if condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard