How to find substring inside a string (or how to grep a variable)? [duplicate]
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
You can also compare with wildcards:
if [[ "$LIST" == *"$SOURCE"* ]]
This works in Bash without forking external commands:
function has_substring() {
[[ "$1" != "${2/$1/}" ]]
}
Example usage:
name="hello/world"
if has_substring "$name" "/"
then
echo "Indeed, $name contains a slash!"
fi
If you're using bash you can just say
if grep -q "$SOURCE" <<< "$LIST" ; then
...
fi
You can use "index" if you only want to find a single character, e.g.:
LIST="server1 server2 server3 server4 server5"
SOURCE="3"
if expr index "$LIST" "$SOURCE"; then
echo "match"
exit -1
else
echo "no match"
fi
Output is:
23
match