I want to judge whether a string is in an array. I found the following function on Google. But I can't fully understand the meaning of this function. In this function, match
is assigned to the string to be matched. But where is the array saved? I added some output, and the variable e
is always empty. I checked the meaning of shift
. It shifts the variable to the left and shifts $2
to $1
. But there is no use of $1
later. Does anyone know how this function works? Many thanks.
containsElement () { local e match="$1" echo $match echo $e shift echo $e echo $match for e; do [[ "$e" == "$match" ]] && return 0; done return 1 }array=("something to search for" " for" "a string" "test2000")containsElement "a string" "${array[@]}"echo $?