[Bash] Remove an array element with unset

Created on November 30, 2026

Pass an indexed element to unset to remove it. This example deletes green at index 1. Quote the argument so the shell does not expand the brackets as a filename pattern.

items=(red green blue)
unset 'items[1]'

printf '%s\n' "${items[@]}"
printf 'index=%s\n' "${!items[@]}"
printf 'count=%s\n' "${#items[@]}"
red
blue
index=0
index=2
count=2

The remaining indexes stay at 0 and 2. ${!items[@]} returns the indexes, and ${#items[@]} returns the number of elements.

Rebuild the indexes from zero

Assign the remaining values back to the array if you want consecutive indexes.

items=(red green blue)
unset 'items[1]'
items=("${items[@]}")

printf '%s\n' "${!items[@]}"
printf '%s\n' "${items[1]}"
0
1
blue

Run these indexed array examples in Bash.

References

Bash