1

I'm trying to match a pattern stored in a variable through an if block. As per my understanding, this should be a match and get "Match" echo statement. However, it doesn't seems to be matching.

patern=EOD*psv
file=EOD_test_20160404.psv

if [[ $file == *"$patern" ]]
then
    echo "Match";
else
    echo "No Match";
fi

Output:

No Match

Why doesn't the file variable value match the specified pattern?

1 Answer 1

4

When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching

So when you include double-quotes in the pattern, bash expects to see them in the filename. Drop the double-quotes and/or the leading * for success.

if [[ $file == $patern ]]
then
    echo "Match";
else
    echo "No Match";
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .