Andrzej Sydor
- 1.4k
- 6
- 14
- 32
I have a string in Bash:
string="My string"
string="My string"
How can I test if it contains another string?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
Where ?? is my unknown operator. Do I use echoecho and grep?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
That looks a bit clumsy.
I have a string in Bash:
string="My string"
How can I test if it contains another string?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
Where ?? is my unknown operator. Do I use echo and grep?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
That looks a bit clumsy.
I have a string in Bash:
string="My string"
How can I test if it contains another string?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
Where ?? is my unknown operator. Do I use echo and grep?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
That looks a bit clumsy.
String How to check if a string contains a substring in Bash
Loading
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>).
Peter Mortensen
- 31.3k
- 22
- 110
- 134
Loading
Loading
Loading
Loading
lang-bash