Check if a Bash array contains a value

前端 未结 30 2411
执笔经年
执笔经年 2020-11-22 07:14

In Bash, what is the simplest way to test if an array contains a certain value?

相关标签:
30条回答
  • 2020-11-22 08:08

    After having answered, I read another answer that I particularly liked, but it was flawed and downvoted. I got inspired and here are two new approaches I see viable.

    array=("word" "two words") # let's look for "two words"
    

    using grep and printf:

    (printf '%s\n' "${array[@]}" | grep -x -q "two words") && <run_your_if_found_command_here>
    

    using for:

    (for e in "${array[@]}"; do [[ "$e" == "two words" ]] && exit 0; done; exit 1) && <run_your_if_found_command_here>
    

    For not_found results add || <run_your_if_notfound_command_here>

    0 讨论(0)
  • 2020-11-22 08:09
    a=(b c d)
    
    if printf '%s\0' "${a[@]}" | grep -Fqxz c
    then
      echo 'array “a” contains value “c”'
    fi
    

    If you prefer you can use equivalent long options:

    --fixed-strings --quiet --line-regexp --null-data
    
    0 讨论(0)
  • 2020-11-22 08:10

    This is working for me:

    # traditional system call return values-- used in an `if`, this will be true when returning 0. Very Odd.
    contains () {
        # odd syntax here for passing array parameters: http://stackoverflow.com/questions/8082947/how-to-pass-an-array-to-a-bash-function
        local list=$1[@]
        local elem=$2
    
        # echo "list" ${!list}
        # echo "elem" $elem
    
        for i in "${!list}"
        do
            # echo "Checking to see if" "$i" "is the same as" "${elem}"
            if [ "$i" == "${elem}" ] ; then
                # echo "$i" "was the same as" "${elem}"
                return 0
            fi
        done
    
        # echo "Could not find element"
        return 1
    }
    

    Example call:

    arr=("abc" "xyz" "123")
    if contains arr "abcx"; then
        echo "Yes"
    else
        echo "No"
    fi
    
    0 讨论(0)
  • 2020-11-22 08:10

    given :

    array=("something to search for" "a string" "test2000")
    elem="a string"
    

    then a simple check of :

    if c=$'\x1E' && p="${c}${elem} ${c}" && [[ ! "${array[@]/#/${c}} ${c}" =~ $p ]]; then
      echo "$elem exists in array"
    fi
    

    where

    c is element separator
    p is regex pattern
    

    (The reason for assigning p separately, rather than using the expression directly inside [[ ]] is to maintain compatibility for bash 4)

    0 讨论(0)
  • 2020-11-22 08:13
    $ myarray=(one two three)
    $ case "${myarray[@]}" in  *"two"*) echo "found" ;; esac
    found
    
    0 讨论(0)
  • 2020-11-22 08:13

    I typically just use:

    inarray=$(echo ${haystack[@]} | grep -o "needle" | wc -w)
    

    non zero value indicates a match was found.

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