Here’s a little function to verify if an element exists in a Bash array.
function elementExists() {
elements=${1}
element=${2}
for i in ${elements[@]} ; do
if [ $i == $element ] ; then
return 1
fi
done
return 0
}
It takes an array as first argument and the element as the second and will return 1 if the element exists in the array. You can now call the function like this:
elementExists my_array my_element