Bash Check if Array Contains Element


Bash Check if Array Contains Element

In Bash scripting, checking if an array contains a specific element is useful for various tasks that require validating the presence of elements within arrays.


Syntax

if [[ " ${array[@]} " =~ " $element " ]]; then
    # commands if element is found
fi

The basic syntax involves using the double brackets [[ ]] and the =~ operator to check if the element is present in the array.


Example Bash Check if Array Contains Element

Let's look at some examples of how to check if an array contains a specific element in Bash:

1. Check if Array Contains a Specific Element

This script initializes an array with elements and checks if it contains the element 'apple', then prints a corresponding message.

#!/bin/bash

array=("apple" "banana" "cherry")
element="apple"

if [[ " ${array[@]} " =~ " $element " ]]; then
    echo "The array contains '$element'."
else
    echo "The array does not contain '$element'."
fi

In this script, the array variable array is initialized with the elements 'apple', 'banana', and 'cherry'. The variable element is assigned the value 'apple'. The if statement uses the [[ ]] and =~ operator to check if element is present in array. If true, it prints a message indicating that the array contains the element. Otherwise, it prints a different message.

Check if array contains a specific element in Bash

2. Check if User-Specified Element is in Array

This script initializes an array with elements, prompts the user to enter an element, checks if the array contains the user-specified element, and prints a corresponding message.

#!/bin/bash

array=("apple" "banana" "cherry")
read -p "Enter the element to check: " element

if [[ " ${array[@]} " =~ " $element " ]]; then
    echo "The array contains '$element'."
else
    echo "The array does not contain '$element'."
fi

In this script, the array variable array is initialized with the elements 'apple', 'banana', and 'cherry'. The user is prompted to enter an element, which is stored in the variable element. The if statement uses the [[ ]] and =~ operator to check if element is present in array. If true, it prints a message indicating that the array contains the element. Otherwise, it prints a different message.

Check if user-specified element is in array in Bash

3. Check if Array from Command Output Contains an Element

This script initializes an array with elements from command output, checks if it contains the element 'conf', and prints a corresponding message.

#!/bin/bash

array=($(ls /etc))
element="conf"

if [[ " ${array[@]} " =~ " $element " ]]; then
    echo "The array contains '$element'."
else
    echo "The array does not contain '$element'."
fi

In this script, the array variable array is initialized with the elements from the output of the ls /etc command, which lists the files and directories in the /etc directory. The variable element is assigned the value 'conf'. The if statement uses the [[ ]] and =~ operator to check if element is present in array. If true, it prints a message indicating that the array contains the element. Otherwise, it prints a different message.

Check if array from command output contains an element in Bash

Conclusion

Checking if an array contains a specific element in Bash is a fundamental task for validating the presence of elements within arrays in shell scripting. Understanding how to check for elements in arrays can help you manage and manipulate arrays effectively in your scripts.