In array operator in bash

A for loop will do the trick.

array=(one two three)

for i in "${array[@]}"; do
  if [[ "$i" = "one" ]]; then
    ...
    break
  fi
done

Try this:

array=(one two three)
if [[ "${array[*]}" =~ "one" ]]; then
  echo "'one' is found"
fi

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

Tags:

Arrays

Shell

Bash