In Bash, what is the simplest way to test if an array contains a certain value?
if ( dlm=$'\x1F' ; IFS="$dlm" ; [[ "$dlm${array[*]}$dlm" == *"$dlm${item}$dlm"* ]] ) ; then
echo "array contains '$item'"
else
echo "array does not contain '$item'"
fi
This approach uses neither external utilities like grep
nor loops.
What happens here, is:
IFS
variable value;IFS
value replacement temporary by evaluating our conditional expression in a sub-shell (inside a pair of parentheses)