← Back to Bash Course | Chapter 6: Arrays | Lesson 9 of 10

Array Length and Removing Elements

You can always ask an array how many elements it has, and you can remove individual elements or wipe the whole array clean when you no longer need them.

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

bash
#!/bin/bash
queue=(a b c d)
echo "Queue length: ${#queue[@]}"

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

bash
#!/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

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

bash
#!/bin/bash
temp_list=(x y z)
echo "Before: ${temp_list[@]}"
unset temp_list
echo "After unset, element count: ${#temp_list[@]}"

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

bash
#!/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[@]}"
Common Mistakes
  1. 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.
  2. Confusing unset arr[idx] (removes one element) with unset arr (removes the entire array and all its elements).
  3. 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).
Chapter Summary
  • ${#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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.