In array operator in bash

前端 未结 9 2573
无人及你
无人及你 2021-02-19 22:20

Is there a way to test whether an array contains a specified element?

e.g., something like:

array=(one two three)

if [ \"one\" in ${array} ]; then
...
f         


        
9条回答
  •  星月不相逢
    2021-02-19 22:31

    A for loop will do the trick.

    array=(one two three)
    
    for i in "${array[@]}"; do
      if [[ "$i" = "one" ]]; then
        ...
        break
      fi
    done
    

提交回复
热议问题