How to delete an exact element in a bash array?
Use the unset
command with the array value at index
, something like this:
#!/usr/bin/env bash
ARRAY=(foo bar any red alpha number of keywords rd3.0 and)
keywords=(red, rednet, rd3.0)
index=0
for keyword in ${ARRAY[@]}; do
if [ "$keyword" = "red" ] || [ "$keyword" = "rd3.0" ] || [ "$keyword" = "rednet" ]; then
# HERE IS TROUBLE
# ARRAY=( ${ARRAY[@]/"$p"/} )
unset ARRAY[$index]
echo "ARRAY is now: ${ARRAY[@]}"
break
fi
let index++
done