Check if a Bash array contains a value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Bash arrays do not have a built-in contains method, so membership checks need to be written explicitly. The safest answer is usually a small loop that compares each element exactly.
There are shorter tricks, but many of them break when values contain spaces, glob characters, or substrings of other values. For shell code, correctness matters more than cleverness.
The Safe Way: Loop and Compare
If you want to know whether an indexed array contains one exact value, loop through the elements and compare with [[ ... == ... ]].
This works because each array element is passed as a separate argument. Quoted expansion with "${fruits[@]}" preserves spaces inside elements.
Why the One-Line Regex Trick Is Risky
You will often see code like this:
It looks compact, but it is fragile. It can produce 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 that contains apple should usually return false for an exact membership test, but string-based tricks can easily report a match anyway.
If the goal is exact array membership, explicit iteration is easier to trust and easier to maintain.
Optimizing Repeated Lookups
If you need to test membership many times, convert the values into an associative array and use the keys as a lookup table.
This approach is useful when one array is fixed and many membership checks follow. The setup costs more up front, but every later lookup is simple and clear.
The ${seen[banana]+x} pattern is important. It checks whether the key exists, even if the stored value is empty. That is more reliable than testing the raw value directly.
Indexed Arrays Versus Associative Arrays
An indexed array stores values by position. An associative array stores values by key. If your task is fundamentally "check whether this label has been seen before," an associative array may be the better data structure from the start.
In other words:
- use an indexed array when order matters
- use an associative array when membership matters
Shell scripts often start with indexed arrays because they are easier to type, then become awkward once repeated membership checks appear. That is a sign the data structure should change.
Common Pitfalls
- Expanding arrays with
${array[*]}when you needed"${array[@]}". - Using substring or regex tricks for exact membership checks.
- Forgetting that array elements may contain spaces or special characters.
- Testing associative-array membership by value instead of by key existence.
Summary
- Bash has no built-in array
containsfunction. - The safest exact membership test is a loop over
"${array[@]}". - Short regex-based tricks are compact but often fragile.
- For repeated lookups, an associative array is usually the better approach.
- Quoting and exact comparison matter when array elements contain spaces or special characters.

