BASH Programming

How to Check if a Bash Array Contains a Value

Bash is a widely used Unix shell that provides a set of powerful tools for system administration and automation. One of the most commonly used programming structures in Bash scripting is an array, which allows you to store multiple values in a single variable, this article, will discuss how to check if a Bash array contains a specific value.

How To Check if Bash Array Contains a Value

Here are three distinct methods you can use to determine whether an array in Bash includes a value:

Method 1: Using a Loop

One way to check if a Bash array contains a value is to iterate over the array using a for loop which compares every element with the value you want to find, here is an example:

#!/bin/bash
cars=("BMW" "VOLVO" "KIA")
car_to_find="KIA"

for car in "${cars[@]}"
do
  if [ "$car" == "$car_to_find" ]
  then
    echo "Found $car!"
    break
  fi
done

 
Here I have an array of car brands and I want to find the brand “KIA” so I iterate over the array using a for loop and compare each car brand with the brand I want to find. If we find a match, we print a message and exit the loop using the break statement.

Method 2: Using the grep Command

Using the grep command to look for the value in the array is another technique to determine whether a Bash array has a value, here is an illustration:

#!/bin/bash

cars=("BMW" "VOLVO" "KIA")
car_to_find="KIA"

if echo "${cars[@]}" | grep -qw "$car_to_find"; then
  echo "Found $car_to_find!"
else
  echo "$car_to_find not found."
fi

 
Here, we used the echo command to print the array to standard output and pipe it to grep. The -q option tells grep to be quiet and only return a status code indicating whether the pattern was found or not. The -w option tells grep to match the pattern as a whole word. If grep finds the pattern, the if statement prints a message indicating that the value was found.

Method 3: Using ${array[@]/pattern/replacement} Syntax

A third way to check if a Bash array contains a value is to use the ${array[@]/pattern/replacement} syntax to replace the value you want to find with a different string, and then compare the resulting array with the original array. Here’s an example:

#!/bin/bash
cars=("BMW" "VOLVO" "KIA")
car_to_find="KIA"
if [[ "${cars[@]/$car_to_find/}" != "${cars[@]}" ]]; then
  echo "Found $car_to_find!"
else
  echo "$car_to_find not found."
fi

 
Here, we use the ${array[@]/pattern/replacement} syntax to remove the value we want to find from the array and if the resulting array is different from the original array, it means that the value was found.

Conclusion

We have discussed three different methods to check if a Bash array contains a value that are: using a loop, using the grep command and using ${array[@]/pattern/replacement} syntax. By using these techniques, you can efficiently search through Bash arrays and perform the necessary operations on the values you find.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.