Is there a way to test whether an array contains a specified element?
e.g., something like:
array=(one two three)
if [ \"one\" in ${array} ]; then
...
f
I got an function 'contains' in my .bashrc-file:
contains ()
{
param=$1;
shift;
for elem in "$@";
do
[[ "$param" = "$elem" ]] && return 0;
done;
return 1
}
It works well with an array:
contains on $array && echo hit || echo miss
miss
contains one $array && echo hit || echo miss
hit
contains onex $array && echo hit || echo miss
miss
But doesn't need an array:
contains one four two one zero && echo hit || echo miss
hit