I have a string in Bash:
string=\"My string\"
How can I test if it contains another string?
if [ $string ?? \'foo\' ]; then
The accepted answer is best, but since there's more than one way to do it, here's another solution:
if [ "$string" != "${string/foo/}" ]; then
echo "It's there!"
fi
${var/search/replace}
is $var
with the first instance of search
replaced by replace
, if it is found (it doesn't change $var
). If you try to replace foo
by nothing, and the string has changed, then obviously foo
was found.