我在 Bash 中有一个字符串:
string="My string"如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
  echo "It's there!"
fi哪里??是我未知的运营商。我使用 echo 和grep吗? 
if echo "$string" | grep 'foo'; then
  echo "It's there!"
fi这看起来有点笨拙。
如果使用双括号,您也可以在案例陈述之外使用Marcus 的答案(* 通配符) :
string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!"
fi请注意,针线中的空格需要放在双引号之间, *通配符应该在外面。 
如果您更喜欢正则表达式方法:
string='My string';
if [[ $string =~ .*My.* ]]
then
   echo "It's there!"
fi我不确定使用 if 语句,但是你可以使用 case 语句获得类似的效果:
case "$string" in 
  *foo*)
    # Do stuff
    ;;
esac