How to check if a string contains a substring in Bash

后端 未结 26 1899
慢半拍i
慢半拍i 2020-11-22 01:58

I have a string in Bash:

string=\"My string\"

How can I test if it contains another string?

if [ $string ?? \'foo\' ]; then         


        
26条回答
  •  悲&欢浪女
    2020-11-22 02:19

    case $string in (*foo*)
      # Do stuff
    esac
    

    This is the same answer as https://stackoverflow.com/a/229585/11267590. But simple style and also POSIX Compliant.

提交回复
热议问题