Array Length and Removing Elements
In this page:
Getting the Current Length
${#arr[@]} always reflects the actual number of elements currently stored in the array, which is important to remember for sparse arrays where the highest index can be larger than the element count. This is the standard way to check an array's size before looping or validating input.
Example: Getting the Current Length
#!/bin/bash
queue=(a b c d)
echo "Queue length: ${#queue[@]}"
Login to try C/C++/Java/PHP code in the editor
Removing a Single Element
unset arr[idx] deletes the element at that specific index, but it leaves a gap there rather than shifting later elements down to close it. This means the array becomes sparse and ${#arr[@]} decreases by exactly one.
Example: Removing a Single Element
#!/bin/bash
fruits=(apple banana cherry date)
unset 'fruits[1]'
echo "After removing index 1: ${fruits[@]}"
echo "Remaining count: ${#fruits[@]}"
for i in "${!fruits[@]}"; do
echo " index $i -> ${fruits[i]}"
done
Login to try C/C++/Java/PHP code in the editor
Removing the Entire Array
unset arr (with no index) deletes the whole array, including every element and the variable itself, as if it had never been declared. This is different from setting arr=(), which empties the array but leaves it declared as an (empty) array.
Example: Removing the Entire Array
#!/bin/bash
temp_list=(x y z)
echo "Before: ${temp_list[@]}"
unset temp_list
echo "After unset, element count: ${#temp_list[@]}"
Login to try C/C++/Java/PHP code in the editor
Compacting a Sparse Array
Reassigning an array to itself via arr=("${arr[@]}") rebuilds it from just its currently-existing elements, discarding any gaps and renumbering everything starting from index 0. This is the standard trick to compact an array after removing elements from the middle.
Example: Compacting a Sparse Array
#!/bin/bash
colors=(red green blue yellow)
unset 'colors[1]'
echo "Sparse: indices are ${!colors[@]}"
colors=("${colors[@]}")
echo "Compacted: indices are ${!colors[@]}, values are ${colors[@]}"
Login to try C/C++/Java/PHP code in the editor
- Assuming
unset arr[idx]shifts later elements down to fill the gap; it does not -- it leaves a hole at that index, making the array sparse rather than renumbering it. - Confusing
unset arr[idx](removes one element) withunset arr(removes the entire array and all its elements). - Expecting
${#arr[@]}to reflect the highest index plus one; for a sparse array, it reflects the actual number of elements present, which can be less than (highest index + 1).
${#arr[@]}gives the current count of elements actually present in the array.unset arr[idx]removes just that one element, leaving a gap rather than shifting subsequent elements down.unset arr(no index) removes the entire array and everything in it.- To truly compact a sparse array (remove gaps and renumber), reassign it via
arr=("${arr[@]}"), which rebuilds it as a fresh, densely-indexed array.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: