如何检查字符串是否在Bash中包含子字符串 - How to check if a string contains a substring in Bash

问题:

I have a string in Bash: 我在Bash中有一个字符串: 测试

string="My string"

How can I test if it contains another string? 如何测试它是否包含另外一个字符串? spa

if [ $string ?? 'foo' ]; then
  echo "It's there!"
fi

Where ?? 哪里?? is my unknown operator. 是我未知的运算符。 Do I use echo and grep ? 我是否使用echo和grep .net

if echo "$string" | grep 'foo'; then
  echo "It's there!"
fi

That looks a bit clumsy. 看起来有点笨拙。 code


解决方案:

参考一: https://stackoom.com/question/xiR/如何检查字符串是否在Bash中包含子字符串
参考二: https://oldbug.net/q/xiR/How-to-check-if-a-string-contains-a-substring-in-Bash
相关文章
相关标签/搜索