Alex Rivera | Logout

Check if a Bash array contains a value

Asked 2010-09-10T15:31:33.790
852

In Bash, what is the simplest way to test if an array contains a certain value?

Edit
Report

1 Answer

79
for i in "${array[@]}"
do
    if [ "$i" -eq "$yourValue" ] ; then
        echo "Found"
    fi
done

For strings:

for i in "${array[@]}"
do
    if [ "$i" == "$yourValue" ] ; then
        echo "Found"
    fi
done
answered 2010-09-10T15:42:23.713

Your Answer