To delete an array element by number use unset
. Examples:
unset 'array[2]'
unset "array[$n]"
unset 'array[n]'
The quotes are essential in all cases. See Bash Pitfalls #57 (unset unquoted array element).
Examples 2 and 3 have exactly the same effect (assuming that the value of variable n
is a valid array index). Example 3 works because Bash doesn't require a preceding $
to expand variables in contexts that support arithmetic, including array indexes.
Note that using unset
on an array index does not change other array indexes. It produces an array with holes in it (aka a sparse array). See Arrays [Bash Hackers Wiki] for more information.
echo ${array[@]}
is broken in several ways. Use Shellcheck to find problems like this. echo "${array[@]}"
is better (and accepted by Shellcheck) but still broken. One way that it can fail is if the first element of the array is a valid echo
option (-n
, -e
, or -E
).
To see exactly what is in array (revealing holes if there are any) use:
declare -p array
For example
array=(a b c d)
declare -p array
n=2
unset 'array[n]'
declare -p array
outputs
declare -a array=([0]="a" [1]="b" [2]="c" [3]="d")
declare -a array=([0]="a" [1]="b" [3]="d")
(note the missing index 2 in the second output line).
Beware that array=( "${array[@]/$x}" )
does not delete any array element. It just removes the first occurrence of the string in $x
from each element in the array.
For example
array=(12321 32123)
declare -p array
x=2
array=( "${array[@]/$x}" )
declare -p array
outputs
declare -a array=([0]="12321" [1]="32123")
declare -a array=([0]="1321" [1]="3123")
The example in the question
declare -a array=(1 2 3 4)
x=2
array=( "${array[@]/$x}" )
declare -p array
outputs
declare -a array=([0]="1" [1]="" [2]="3" [3]="4")
The second array element (index 1) has not been removed. It's just been set to the empty string.
There's some useful information in Remove an element from a Bash array. However, the question is about deleting array elements with particular values, not deleting elements by index. Also, a lot of the answers (including the accepted (and much-upvoted) one) have serious problems. Many of the problems are identified in comments, but some of the comments are also very wrong. Be careful.