How to check if a string contains a substring in Bash

后端 未结 26 1876
慢半拍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:27

    msg="message"
    
    function check {
        echo $msg | egrep [abc] 1> /dev/null
    
        if [ $? -ne 1 ];
        then 
            echo "found" 
        else 
            echo "not found" 
        fi
    }
    
    check
    

    This will find any occurance of a or b or c

提交回复
热议问题