Link to home
Start Free TrialLog in
Avatar of pkunwor
pkunwor

asked on

Check if string/variable contains another string

How can I check if a variable contains another string in unix
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

a="fullstring"
b="searchstring"
[ $(echo "$a" | grep -c "$b") -gt 0 ] && echo "String found!" || echo "String not found!"
which shell?
Avatar of pkunwor
pkunwor

ASKER

korn shell

Above code worked, however i am trying to use with if statement and not able to make above work.

e.g. if fullstring contains searchstring then
do st
else
do st else
fi
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pkunwor

ASKER

i was doing similar if before and was not working, it is working now. Thanks.