Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash #declare an array my_array=("apple" "banana" "cherry" "date") #value to search for search_value="banana" #check if the array contains the value with grep if echo "${my_array[@]}" | grep -qw "$search_value"; then echo "Array contains $search_value" exit 0 else echo "Array does not contain $search_value" exit 1 fi |
1 2 3 |
Array contains banana |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash #declare an array my_array=("apple" "banana" "cherry" "date") #value to search for search_value="cherry" #check if the array contains the value with grep if [[ " ${my_array[@]} " =~ " ${search_value} " ]]; then echo "Array contains $search_value" exit 0 else echo "Array does not contain $search_value" exit 1 fi |
1 2 3 |
Array contains cherry |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash #declare an array my_array=("apple" "banana" "cherry" "date") #value to search for search_value="banana" #loop through the array for i in "${my_array[@]}" do #check if the element matches the search value if [ "$i" == "$search_value" ] then echo "Array contains $search_value" exit 0 fi done echo "Array does not contain $search_value" exit 1 |
1 2 3 |
Array contains banana |
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_value
variable. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash #declare an array my_array=("apple" "banana" "cherry" "date") #value to search for search_value="banana" #check if the array contains the value with the = operator if [[ " ${my_array[*]} " = *" $search_value "* ]]; then echo "Array contains $search_value" exit 0 else echo "Array does not contain $search_value" exit 1 fi |
1 2 3 |
Array contains banana |
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 iscar
ormotorcycle
. - This method works for both
indexed
andassociative
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.