Check if a Bash array contains a value

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

    containsElement () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
    

    Now handles empty arrays correctly.

提交回复
热议问题