Check if a Bash array contains a value

前端 未结 30 2733
执笔经年
执笔经年 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 08:09

    a=(b c d)
    
    if printf '%s\0' "${a[@]}" | grep -Fqxz c
    then
      echo 'array “a” contains value “c”'
    fi
    

    If you prefer you can use equivalent long options:

    --fixed-strings --quiet --line-regexp --null-data
    

提交回复
热议问题