Quantcast
Channel: How does this function accessing an array work? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

How does this function accessing an array work?

$
0
0

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 $?

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>