Bash Scripting
Programming
Arrays
Code Snippets
Command Line Tools

Check if a Bash array contains a value

Codemia Knowledge Hub ·

Bash arrays have no built-in contains method. The safest check is a small loop that compares each element exactly with [[ ... == ... ]]. Shorter one-liners exist, but many break when values contain spaces, glob characters, or substrings of other values.

Loop and compare each element

bash
1#!/usr/bin/env bash
2
3contains_value() {
4  local target="$1"
5  shift
6
7  local item
8  for item in "$@"; do
9    if [[ "$item" == "$target" ]]; then
10      return 0
11    fi
12  done
13
14  return 1
15}
16
17fruits=("apple" "banana split" "pear")
18
19if contains_value "banana split" "${fruits[@]}"; then
20  echo "found"
21else
22  echo "not found"
23fi
text
found

Each array element is passed as a separate argument, and the quoted expansion "${fruits[@]}" preserves spaces inside elements.

Why the one-line regex trick is risky

You will often see this:

bash
[[ " ${array[*]} " =~ " $value " ]]

It is compact but fragile, and can give false positives when:

  • one element is a substring of another
  • values contain spaces
  • values contain regex metacharacters
  • globbing and word splitting are not handled carefully

For example, searching for app in an array containing apple should be false for exact membership, but string tricks can easily report a match. For exact membership, explicit iteration is easier to trust and maintain.

Associative array for repeated lookups

If you test membership many times, build an associative array once and use its keys as a lookup table:

bash
1#!/usr/bin/env bash
2
3fruits=("apple" "banana" "pear")
4declare -A seen=()
5
6for item in "${fruits[@]}"; do
7  seen["$item"]=1
8done
9
10if [[ -n "${seen[banana]+x}" ]]; then
11  echo "found"
12fi

Setup costs more up front, but every later lookup is simple. The ${seen[banana]+x} pattern checks whether the key exists even if its value is empty, which is more reliable than testing the value directly.

Indexed or associative from the start

An indexed array stores values by position; an associative array stores them by key. Use an indexed array when order matters, and an associative array when membership matters. Scripts often start with indexed arrays and become awkward once repeated membership checks appear, which is a sign the data structure should change.

Mistakes to avoid

  • Expanding with ${array[*]} when you needed "${array[@]}".
  • Using substring or regex tricks for exact membership checks.
  • Forgetting that elements may contain spaces or special characters.
  • Testing associative-array membership by value instead of by key existence.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms