Bash IF var contains a certain string

Hello

I am having trouble finding a way to check if a variable contains a certain string

what I want is something like that

if [ $variable contains "serach_text"]
then

fi

I know a cheap and easy way of doing this would be
but I was looking for other ways
if [ echo $variable | grep "search_text]
then

fi


Thanks for your time and help
LVL 2
jculkincysAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ravenplCommented:
if [ _$var = "_search text" ]; then
...
ravenplCommented:
posibly better
if [ "_$var" = "_search text" ]; then
jculkincysAuthor Commented:
Can you please explain a little what the underscore does?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jculkincysAuthor Commented:
Will your solution will let me know if the variable contains the search text or will it tell me if the variable is equal t the search text.
ravenplCommented:
> Can you please explain a little what the underscore does?
supresses error if $var is empty

> Will your solution will let me know if the variable contains the search text or will it tell me if the variable is equal t the search text.
Right - it checks for equality.

if [[ -n "$var" && $var == *searched\ text* ]]; then
jculkincysAuthor Commented:
Alright

Is there any easy way to check if the variable contains the search string

Here is a better example

$var = "ravenpl answer"
if [$var contains "answer"]
then
        give him the points and an A
gi
ravenplCommented:
I already have showed that. Haven't I?

if [[ -n "$var" && $var == *answer* ]]

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
jculkincysAuthor Commented:
Oh my mistake - I missed it when you posted it the first time
jculkincysAuthor Commented:
may I ask why you have "$var" && $var?
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

From novice to tech pro — start learning today.