[Bash] Check whether an array contains an element

Created on November 30, 2026

Loop over the array and use [[ ... == ... ]] to compare each element with the target.

fruits=("apple" "banana" "orange")
target="banana"
found=false

for fruit in "${fruits[@]}"; do
  if [[ "$fruit" == "$target" ]]; then
    found=true
    break
  fi
done

echo "$found"

Output

true

"${fruits[@]}" expands each array element as a separate value. Quoting "$target" makes the comparison literal, including when the value contains spaces.

Bash Shell command