How to check if string exists in bash variable

Viewed 131

I have a variable $FOO with values one two three.

echo $FOO
one two three

Is there a way in bash to check if two exists and exactly matches one of the values of $FOO

I can do this

if [[ "two" =~ ^(one|two|three)$ ]]; then
    echo "two is in the list"
else
    echo "two is not in the list"
fi

And it works, but I'd like to check using the actual variable $FOO and not (one|two|three)$

Is this possible?

7 Answers

If the items in $FOO are guaranteed to be separated by spaces, you can use this:

if [[ " $FOO " = *" $TWO "* ]]; then
    ...

Note that this reverses the roles of $FOO and $TWO from what you might expect; it treats $FOO as the string to check against a pattern, and the pattern is essentially "something that contains $TWO". It also adds explicit spaces to $TWO to make sure it matches only full items, and similarly adds spaces around $FOO so that'll match the first and last items in it.

If the delimiters in $FOO might be tabs or other space-like characters, you can use this more complex pattern:

if [[ " $FOO " = *[[:space:]]"$TWO"[[:space:]]* ]]; then

Heres how to do it below

FOO='one two three'
TWO='two'
if [[ "$FOO" == *"$TWO"* ]]; then
  echo "two is in the list"
fi

wildcard aka * is a symbol used to represent zero, one or more characters.

Using an array

$ FOO=(one two three)
$ n=0;while [[ $n -lt ${#FOO} ]]; do  [[ "${FOO[$n]}" == "two" ]] && echo "two is in the list"; n=$(( $n + 1 )); done
two is in the list

You can construct pattern with :

foo=(one two three)
pattern="^($(IFS="|"; echo "${foo[*]}"))\$"

if  [[ two =~ $pattern ]]; then
    echo "two is in the list"
else
    echo "two is not in the list"
fi

It's odd that nobody posted case. It's the simplest and most portable. Notice the added spaces around $FOO.

case " $FOO " in
*" two "*) echo two is in the list; ;;
*) echo two is not in the list; ;;
esac

You can try something like this

FOO="one two three"

foo=( $FOO )

if [[ "${foo[*]}" =~ ' two ' ]]
then
    echo "two is in the list"
else
    echo "two is not in the list"
fi

which will work for the simple cases (i.e. no spaces in the values)

This should also work for you.

FOO='one two three'
TWO='two'
if [[ "$FOO" =~ "$TWO"( |$) ]]
then
  echo "two is in the list"
else
    echo "two is not in the list"
fi

This will give an exact pattern match.

Related