arr=("alice" "bob" "carol")
target="bob"
# Method 1: explicit loop (clearest)
contains() {
local needle="$1"; shift
for x in "$@"; do
[[ "$x" == "$needle" ]] && return 0
done
return 1
}
if contains "$target" "${arr[@]}"; then
echo "yes"
fi
# Method 2: glue + regex (concise but tricky with separators)
[[ " ${arr[*]} " =~ " $target " ]] && echo "yes (regex)"
Create a free account and build your private vault. Share publicly whenever you want.