Check if a Bash array contains a value

前端 未结 30 2981
执笔经年
执笔经年 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 07:52

    for i in "${array[@]}"
    do
        if [ "$i" -eq "$yourValue" ] ; then
            echo "Found"
        fi
    done
    

    For strings:

    for i in "${array[@]}"
    do
        if [ "$i" == "$yourValue" ] ; then
            echo "Found"
        fi
    done
    

提交回复
热议问题