bash scripting - search a match in a string

I have a string on a variable, I want to search for two matches.. something like this:

var="Take the red pill, or the blue pill"

if [ "red pill" isin $var ]; <-- this I don't know
then
    echo "Somebody mentioned the red pill"
else
    echo "Somebody said something but not about the red pill"
fi

I googled for "search string" and stuff but I get awk and expr funtions that I don't seem to understand (or indeed it isn't me and the result of my search aren't right). Maybe I'm looking wrong. :S

Thaaanks experts :)
LVL 2
mmarthaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blklineCommented:
Someone with better regexp skills will undoubtedly jump in here but here's a quicky:

if echo $var | egrep -e '(^| )red( |$)'; then
   echo "Somebody mentioned the red pill"
else
   echo "Somebody said something but not about the red pill"
fi

We're looking for:

beginning of the line or a space, followed by the letters r e d followed by either a space or end-of-line.

Of course, punctuation will cause a problem after the word red in this example, so you could allow alter the regex slightly to allow non-character trailers after r e d:

'(^| )red([^:alpha:]| |$)'

Is this what you were looking for?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TintinCommented:
if echo "$var" | grep -qw "red pill'
then
    echo "Somebody mentioned the red pill"
else
   echo "Somebody said something but not about the red pill"
fi

The -w flag says to search for whole words and the -q flag is for quiet (ie. no output)
0
mmarthaAuthor Commented:
thank you both. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.