In Bash, what is the simplest way to test if an array contains a certain value?
After having answered, I read another answer that I particularly liked, but it was flawed and downvoted. I got inspired and here are two new approaches I see viable.
array=("word" "two words") # let's look for "two words"
grep
and printf
:(printf '%s\n' "${array[@]}" | grep -x -q "two words") &&
for
:(for e in "${array[@]}"; do [[ "$e" == "two words" ]] && exit 0; done; exit 1) &&
For not_found results add ||