How to check if a string contains a substring in Bash

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

    My .bash_profile file and how I used grep:

    If the PATH environment variable includes my two bin directories, don't append them,

    # .bash_profile
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    
    U=~/.local.bin:~/bin
    
    if ! echo "$PATH" | grep -q "home"; then
        export PATH=$PATH:${U}
    fi
    
    0 讨论(0)
  • 2020-11-22 02:17

    I found to need this functionality quite frequently, so I'm using a home-made shell function in my .bashrc like this which allows me to reuse it as often as I need to, with an easy to remember name:

    function stringinstring()
    {
        case "$2" in
           *"$1"*)
              return 0
           ;;
        esac
        return 1
    }
    

    To test if $string1 (say, abc) is contained in $string2 (say, 123abcABC) I just need to run stringinstring "$string1" "$string2" and check for the return value, for example

    stringinstring "$str1" "$str2"  &&  echo YES  ||  echo NO
    
    0 讨论(0)
  • 2020-11-22 02:19

    You should remember that shell scripting is less of a language and more of a collection of commands. Instinctively you think that this "language" requires you to follow an if with a [ or a [[. Both of those are just commands that return an exit status indicating success or failure (just like every other command). For that reason I'd use grep, and not the [ command.

    Just do:

    if grep -q foo <<<"$string"; then
        echo "It's there"
    fi
    

    Now that you are thinking of if as testing the exit status of the command that follows it (complete with semi-colon), why not reconsider the source of the string you are testing?

    ## Instead of this
    filetype="$(file -b "$1")"
    if grep -q "tar archive" <<<"$filetype"; then
    #...
    
    ## Simply do this
    if file -b "$1" | grep -q "tar archive"; then
    #...
    

    The -q option makes grep not output anything, as we only want the return code. <<< makes the shell expand the next word and use it as the input to the command, a one-line version of the << here document (I'm not sure whether this is standard or a Bashism).

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

    This Stack Overflow answer was the only one to trap space and dash characters:

    # For null cmd arguments checking   
    to_check=' -t'
    space_n_dash_chars=' -'
    [[ $to_check == *"$space_n_dash_chars"* ]] && echo found
    
    0 讨论(0)
  • 2020-11-22 02:19

    grep -q is useful for this purpose.

    The same using awk:

    string="unix-bash 2389"
    character="@"
    printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
    

    Output:

    Not Found

    string="unix-bash 2389"
    character="-"
    printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
    

    Output:

    Found

    Original source: http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题