字符串包含在巴什 [英] String contains in Bash
本文介绍了字符串包含在巴什的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
,我有一个字符串:
string = "My string"
我
如何测试是否包含另一个字符串?
How can I test if it contains another string?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
其中, ??
是我未知的运营商。难道我用的回声和的grep
?
Where ??
is my unknown operator. Do I use echo and grep
?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
这看起来有点笨拙。
推荐答案
您可以使用马库斯的回答(*通配符) case语句外也一样,如果你使用双括号:
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 the double quotes.
这篇关于字符串包含在巴什的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文