In array operator in bash

前端 未结 9 1897
无人及你
无人及你 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:43

    array="one two three"
    if [ $(echo "$array" | grep one | wc -l) -gt 0 ] ; 
      then echo yes; 
    fi
    

    If that's ugly, you could hide it away in a function.

提交回复
热议问题