0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check if Array Contains Element

Use a loop with if [[ "$element" == "$item" ]] inside to check each array item; for example, for item in "${array[@]}"; do if [[ "$item" == "$element" ]]; then echo "Found"; fi; done.
📋

Examples

Inputarray=(apple banana cherry); element=banana
OutputFound
Inputarray=(dog cat mouse); element=elephant
OutputNot Found
Inputarray=(); element=anything
OutputNot Found
🧠

How to Think About It

To check if an array contains an element, look at each item one by one and compare it to the element you want. If any item matches, you know the element is in the array. If you finish checking all items without a match, the element is not there.
📐

Algorithm

1
Get the array and the element to find.
2
Loop through each item in the array.
3
Compare the current item with the element.
4
If they match, print 'Found' and stop checking.
5
If no match is found after checking all items, print 'Not Found'.
💻

Code

bash
array=(apple banana cherry)
element=banana
found=0
for item in "${array[@]}"; do
  if [[ "$item" == "$element" ]]; then
    found=1
    break
  fi
done
if [[ $found -eq 1 ]]; then
  echo "Found"
else
  echo "Not Found"
fi
Output
Found
🔍

Dry Run

Let's trace the example where array=(apple banana cherry) and element=banana through the code

1

Initialize variables

array=(apple banana cherry), element=banana, found=0

2

Check first item

item=apple, compare with banana → no match, continue

3

Check second item

item=banana, compare with banana → match found, set found=1, break loop

Iterationitemfound
1apple0
2banana1
💡

Why This Works

Step 1: Loop through array

The for loop goes through each element in the array one by one.

Step 2: Compare elements

Inside the loop, if [[ "$item" == "$element" ]] checks if the current item matches the element.

Step 3: Set flag and break

If a match is found, we set found=1 and break to stop checking further.

Step 4: Print result

After the loop, we print 'Found' if found is 1, otherwise 'Not Found'.

🔄

Alternative Approaches

Using a function with a loop
bash
contains_element() {
  local e
  for e in "${@:2}"; do
    [[ "$e" == "$1" ]] && return 0
  done
  return 1
}

array=(apple banana cherry)
element=banana
if contains_element "$element" "${array[@]}"; then
  echo "Found"
else
  echo "Not Found"
fi
This method wraps the check in a reusable function, improving readability and reuse.
Using grep with printf
bash
array=(apple banana cherry)
element=banana
if printf "%s\n" "${array[@]}" | grep -qx "$element"; then
  echo "Found"
else
  echo "Not Found"
fi
This uses external grep command to check presence, which is concise but less efficient for large arrays.

Complexity: O(n) time, O(1) space

Time Complexity

The script checks each element once, so time grows linearly with array size.

Space Complexity

Only a few variables are used, so space stays constant regardless of array size.

Which Approach is Fastest?

The loop with break is fastest for large arrays because it stops early; grep is simpler but always checks all elements.

ApproachTimeSpaceBest For
Loop with breakO(n)O(1)Large arrays, early exit
Function wrapperO(n)O(1)Reusable code, clarity
grep with printfO(n)O(n)Small arrays, quick scripts
💡
Use break to stop the loop early once the element is found for better performance.
⚠️
Forgetting to quote variables inside [[ ]] can cause errors or wrong matches.