Posted By: davidsheldon
I have a string in Bash:
How can I test if it contains another string?
if [ $string ?? 'foo' ]; then echo "It's there!" fi
?? is my unknown operator. Do I use
if echo "$string" | grep 'foo'; then echo "It's there!" fi
That looks a bit clumsy.
You can use Marcus’s answer (* wildcards) outside a case statement, too, if you use double brackets:
string='My long string' if [[ $string == *"My long"* ]]; then echo "It's there!" fi
Note that spaces in the needle string need to be placed between double quotes, and the
* wildcards should be outside. Also note that a simple comparison operator is used (i.e.
==), not the regex operator