Array contains a value
Return 0 if a nameref array contains the given value, otherwise 1.
18th August 2026
array_contains() {
local -n haystack=$1
local needle=$2
for i in "${haystack[@]}"; do
if [[ $i == "${needle}" ]]; then
return 0
fi
done
return 1
}
Caveats
Needs Bash namerefs (Bash 4.3+). Pass the array name, not an expansion.