How to check if a string contains a substring in Bash

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

    This also works:

    if printf -- '%s' "$haystack" | egrep -q -- "$needle"
    then
      printf "Found needle in haystack"
    fi
    

    And the negative test is:

    if ! printf -- '%s' "$haystack" | egrep -q -- "$needle"
    then
      echo "Did not find needle in haystack"
    fi
    

    I suppose this style is a bit more classic -- less dependent upon features of Bash shell.

    The -- argument is pure POSIX paranoia, used to protected against input strings similar to options, such as --abc or -a.

    Note: In a tight loop this code will be much slower than using internal Bash shell features, as one (or two) separate processes will be created and connected via pipes.

    0 讨论(0)
  • 2020-11-22 02:03

    Since the POSIX/BusyBox question is closed without providing the right answer (IMHO), I'll post an answer here.

    The shortest possible answer is:

    [ ${_string_##*$_substring_*} ] || echo Substring found!
    

    or

    [ "${_string_##*$_substring_*}" ] || echo 'Substring found!'
    

    Note that the double hash is obligatory with some shells (ash). Above will evaluate [ stringvalue ] when the substring is not found. It returns no error. When the substring is found the result is empty and it evaluates [ ]. This will throw error code 1 since the string is completely substituted (due to *).

    The shortest more common syntax:

    [ -z "${_string_##*$_substring_*}" ] && echo 'Substring found!'
    

    or

    [ -n "${_string_##*$_substring_*}" ] || echo 'Substring found!'
    

    Another one:

    [ "${_string_##$_substring_}" != "$_string_" ] && echo 'Substring found!'
    

    or

    [ "${_string_##$_substring_}" = "$_string_" ] || echo 'Substring found!'
    

    Note the single equal sign!

    0 讨论(0)
  • 2020-11-22 02:03

    The generic needle haystack example is following with variables

    #!/bin/bash
    
    needle="a_needle"
    haystack="a_needle another_needle a_third_needle"
    if [[ $haystack == *"$needle"* ]]; then
        echo "needle found"
    else
        echo "needle NOT found"
    fi
    
    0 讨论(0)
  • 2020-11-22 02:06
    [[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"
    
    0 讨论(0)
  • 2020-11-22 02:07

    Try oobash.

    It is an OO-style string library for Bash 4. It has support for German umlauts. It is written in Bash.

    Many functions are available: -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -matches, -replaceAll, -replaceFirst, -startsWith, -substring, -swapCase, -toLowerCase, -toString, -toUpperCase, -trim, and -zfill.

    Look at the contains example:

    [Desktop]$ String a testXccc
    [Desktop]$ a.contains tX
    true
    [Desktop]$ a.contains XtX
    false
    

    oobash is available at Sourceforge.net.

    0 讨论(0)
  • 2020-11-22 02:10

    How about this:

    text="   <tag>bmnmn</tag>  "
    if [[ "$text" =~ "<tag>" ]]; then
       echo "matched"
    else
       echo "not matched"
    fi
    
    0 讨论(0)
提交回复
热议问题