Check if Array Contains Value in Bash

Check if Array contains value in Bash

Using echo with grep Command

To check if a Bash array contains a value, use the echo command and pipe it to grep command to search the value from the defined array.

The value in the bash can also be checked with the grep command. We first declared an array named my_array that contained four elements, i.e., the name of fruits. In addition, we defined a variable called search_value and initialized it with the value, i.e., "banana".

After that echo command was used to print the contents of the my_array array and piped the output to the grep command. The -w flag directed the grep to search for whole words only, and the -q flag directed it to be quiet and not print any output.

If grep found a match, it returned a success status code (0); after that if statement was used to determine whether to print the Array contains $search_value message and exit with a success status code (0). Otherwise, the message Array does not contain $search_value is printed and exits with a failure status code (1).

Using =~ operator with if-else

To check if Array contains value in Bash, use =~ operator with if-else statement.

Using for Loop

To determine if a Bash array contains a value:

  • Use the for loop to iterate over the array.
  • In each iteration, use the if-else block to check if the specified value exists in the Bash array.

First, we defined an array named my_array with four string-type elements. Next, we declared and initialized the variable search_value with the value to be searched in the my_array; it is banana in this case. After that, we used the for loop to loop through the my_array and checked each element in the array.

Inside the loop, we used the if statement to determine if an element matched the value of the search_valuevariable. If so, the [ "$i" == "$search_value" ] expression will return True and result in executing the if block where we used an echo statement to print a message indicating that the array contained the value and exited the script with a success status code (0).

On the other hand, if we looped through the entire my_array and did not find the search value, we printed a message indicating that the array did not contain the value and exited the script with a failure status code (1).

Using the = Operator

To check if a Bash array contains a value, use the = operator with if-else block to compare and search the value from the defined array.

Last but not least, the = operator is also used to verify the presence of the value in the array by creating a regular expression. Same as above, we declared an array called my_array that contained four elements. Then defined a variable called search_value and set its value to "banana".

Then the [[ " ${my_array[*]} " = *" $search_value "* ]] syntax is used to search value from the array. The " ${my_array[*]} " syntax expanded the array into a single string with every element separated by a space, and the * wildcard characters indicated that the search value could appear at any position in the string.

If the search value was found, the if statement printed the Array contains $search_value message and exited with a success status code (0). Otherwise, it printed the Array did not contain $search_value message and exited with a failure status code (1).

In all cases discussed above, the value was found, so the message was printed that "Array contains banana", and the script was exited with a success status code (0).

Note:

  • The code can be modified to work with any array and search value you want to use. For instance, there is an array consisting of three elements, i.e., the name of vehicles like car, bus, truck and the specific value that needs to be searched is car or motorcycle.
  • This method works for both indexed and associative arrays. In this article, the indexed array is used. If searching through an associative array, you can replace the ${!my_array[@]} syntax to loop through the array keys.

The best approach may depend on the specific situation and personal preference. Still, these three methods are all viable options for checking if a Bash array contains a particular value.

That’s all about how to check if array contains value in Bash.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *