如何檢查字符串是否在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
相關文章
相關標籤/搜索