Check if a Bash array contains a value

前端 未结 30 2753
执笔经年
执笔经年 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:51

    Here is a small contribution :

    array=(word "two words" words)  
    search_string="two"  
    match=$(echo "${array[@]:0}" | grep -o $search_string)  
    [[ ! -z $match ]] && echo "found !"  
    

    Note: this way doesn't distinguish the case "two words" but this is not required in the question.

提交回复
热议问题