How do you tell if a string contains another string in Unix shell scripting?

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-13T19:17:33Z Indexed on 2010/05/13 19:24 UTC
Read the original article Hit count: 230

Filed under:

Hi all,

I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make this not shell specific (i.e. not bash only) but if there's no other way I can make do with that.

#!/bin/sh
CURRENT_DIR=`pwd`

if [ CURRENT_DIR contains "String1" ]
then
 echo "String1 present"
elif [ CURRENT_DIR contains "String1" ]
then
 echo "String2 present"
else
 echo "Else"
fi

© Stack Overflow or respective owner

Related posts about unix-programming