How can I check if a string is in an array without iterating over the elements?

后端 未结 9 2303
眼角桃花
眼角桃花 2021-02-13 18:54

Is there a way of checking if a string exists in an array of strings - without iterating through the array?

For example, given the script below, how I can correctly impl

9条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 19:17

    In most cases, the following would work. Certainly it has restrictions and limitations, but easy to read and understand.

    if [ "$(echo " ${array[@]} " | grep " $test ")" == "" ]; then
        echo notFound
    else
        echo found
    fi
    

提交回复
热议问题