Bash Script to Search Element in Array with Example
Use a
for loop to check each element in the array and compare it with the target using if [[ "$element" == "$target" ]]; if matched, print found and exit.Examples
Inputarray=(apple banana cherry) target=banana
OutputElement 'banana' found in the array.
Inputarray=(dog cat mouse) target=elephant
OutputElement 'elephant' not found in the array.
Inputarray=() target=any
OutputElement 'any' not found in the array.
How to Think About It
To find an element in a Bash array, you look at each item one by one and compare it to the element you want. If you find a match, you stop and say it is found. If you finish checking all items without a match, you say it is not found.
Algorithm
1
Get the array and the target element to search.2
Loop through each element in the array.3
Compare the current element with the target.4
If they match, print found and stop the search.5
If the loop ends without a match, print not found.Code
bash
#!/bin/bash array=(apple banana cherry) target="banana" found=0 for element in "${array[@]}"; do if [[ "$element" == "$target" ]]; then echo "Element '$target' found in the array." found=1 break fi done if [[ $found -eq 0 ]]; then echo "Element '$target' not found in the array." fi
Output
Element 'banana' found in the array.
Dry Run
Let's trace searching for 'banana' in the array (apple, banana, cherry).
1
Start loop with element 'apple'
Compare 'apple' with 'banana' → no match
2
Next element 'banana'
Compare 'banana' with 'banana' → match found, print message and stop
| Element | Comparison Result |
|---|---|
| apple | no |
| banana | yes |
Why This Works
Step 1: Loop through array elements
The for loop goes over each item in the array one by one.
Step 2: Compare each element
Inside the loop, if [[ "$element" == "$target" ]] checks if the current element matches the target.
Step 3: Stop when found
If a match is found, break stops the loop early to save time.
Step 4: Print result
If no match is found after the loop, print that the element was not found.
Alternative Approaches
Using a function to search
bash
#!/bin/bash function contains() { local e for e in "${array[@]}"; do [[ "$e" == "$1" ]] && return 0 done return 1 } array=(apple banana cherry) target="banana" if contains "$target"; then echo "Element '$target' found in the array." else echo "Element '$target' not found in the array." fi
This method wraps the search in a reusable function, improving readability and reuse.
Using grep with printf
bash
#!/bin/bash array=(apple banana cherry) target="banana" if printf "%s\n" "${array[@]}" | grep -qx "$target"; then echo "Element '$target' found in the array." else echo "Element '$target' not found in the array." fi
This uses external grep command, which can be slower but is concise and powerful.
Complexity: O(n) time, O(1) space
Time Complexity
The script checks each element once, so time grows linearly with array size.
Space Complexity
No extra space is used besides a few variables; the search is done in-place.
Which Approach is Fastest?
The loop with break is fastest for small to medium arrays; grep is slower due to process overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop with break | O(n) | O(1) | Simple scripts, small arrays |
| Function wrapper | O(n) | O(1) | Reusable code, readability |
| grep with printf | O(n) | O(n) | Quick one-liners, complex patterns |
Use
break to stop the loop early once the element is found for better performance.Forgetting to quote variables in comparisons, which can cause errors with spaces or special characters.