Check if string/variable contains another string

pkunwor
pkunwor used Ask the Experts™
on
How can I check if a variable contains another string in unix
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

Commented:
a="fullstring"
b="searchstring"
[ $(echo "$a" | grep -c "$b") -gt 0 ] && echo "String found!" || echo "String not found!"
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
which shell?

Author

Commented:
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
Most Valuable Expert 2013
Top Expert 2013
Commented:
a="fullstring"
b="searchstring"
if [ $(echo "$a" | grep -c "$b") -gt 0 ]
  then
    echo "String found!"
  else
     echo "String not found!"
 fi

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial