Check if a variable exists in a list in Bash

Learn check if a variable exists in a list in bash with practical examples, diagrams, and best practices. Covers linux, bash development techniques with visual explanations.

How to Check if a Variable Exists in a List in Bash

Hero image for Check if a variable exists in a list in Bash

Learn various robust methods to determine if a specific value is present within a Bash array or a space-separated string, enhancing your shell scripting capabilities.

Checking for the existence of a variable within a list is a common task in Bash scripting. Whether you're validating user input, managing configurations, or processing data, knowing if an item is present in a collection is fundamental. This article explores several effective techniques, from simple string matching to more advanced array manipulations, providing you with the tools to handle various scenarios.

Method 1: Using [[ ... =~ ... ]] for String Matching

One of the most straightforward ways to check for a variable's presence in a space-separated list is by using Bash's extended test command [[ ... ]] with regular expressions. This method treats the list as a single string and searches for the variable as a word boundary-delimited pattern. This is particularly useful for simple lists where elements are guaranteed to be separated by spaces.

#!/bin/bash

my_list="apple banana cherry date"
search_item="banana"

if [[ " $my_list " =~ " $search_item " ]]; then
  echo "'$search_item' found in the list."
else
  echo "'$search_item' not found in the list."
fi

search_item="grape"

if [[ " $my_list " =~ " $search_item " ]]; then
  echo "'$search_item' found in the list."
else
  echo "'$search_item' not found in the list."
fi

Checking for an item in a space-separated string using regular expressions.

Method 2: Iterating Through a Bash Array

For more structured data, Bash arrays are the preferred choice. You can iterate through each element of an array and compare it with the target variable. This method is explicit and works reliably for any array content, including elements with spaces or special characters, as long as they are properly quoted within the array definition.

#!/bin/bash

my_array=("apple" "banana" "cherry pie" "date")
search_item="cherry pie"
found=0

for item in "${my_array[@]}"; do
  if [[ "$item" == "$search_item" ]]; then
    found=1
    break
  fi
done

if [[ $found -eq 1 ]]; then
  echo "'$search_item' found in the array."
else
  echo "'$search_item' not found in the array."
fi

search_item="grape"
found=0

for item in "${my_array[@]}"; do
  if [[ "$item" == "$search_item" ]]; then
    found=1
    break
  fi
done

if [[ $found -eq 1 ]]; then
  echo "'$search_item' found in the array."
else
  echo "'$search_item' not found in the array."
fi

Iterating through a Bash array to find a specific element.

flowchart TD
    A[Start]
    B{Is list empty?}
    C[Initialize 'found' flag to 0]
    D{Loop through each 'item' in list}
    E{Is 'item' == 'search_item'?}
    F[Set 'found' flag to 1]
    G[Break loop]
    H{Is 'found' flag 1?}
    I[Print 'Found']
    J[Print 'Not Found']
    K[End]

    A --> B
    B -- Yes --> J
    B -- No --> C
    C --> D
    D --> E
    E -- Yes --> F
    F --> G
    E -- No --> D
    G --> H
    D -- Loop End --> H
    H -- Yes --> I
    H -- No --> J
    I --> K
    J --> K

Flowchart illustrating the logic for checking an item's existence in a list.

Method 3: Using grep with printf for Arrays

For larger arrays or when you need more advanced pattern matching capabilities, combining printf with grep can be very efficient. This method prints each array element on a new line and pipes the output to grep, which then searches for the target item. This approach is particularly powerful because grep supports a wide range of regular expressions.

#!/bin/bash

my_array=("apple" "banana" "cherry pie" "date")
search_item="banana"

if printf '%s\n' "${my_array[@]}" | grep -q -x "$search_item"; then
  echo "'$search_item' found in the array."
else
  echo "'$search_item' not found in the array."
fi

search_item="cherry"

if printf '%s\n' "${my_array[@]}" | grep -q "$search_item"; then
  echo "'$search_item' found in the array (partial match).
  Note: Use -x for exact match."
else
  echo "'$search_item' not found in the array."
fi

search_item="grape"

if printf '%s\n' "${my_array[@]}" | grep -q -x "$search_item"; then
  echo "'$search_item' found in the array."
else
  echo "'$search_item' not found in the array."
fi

Using printf and grep to check for an item in a Bash array.