Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all members of an array are equal to something in unix bash

Is there any method without a loop to check whether all members of the following arrays are equal to true?

found1=(true true true true);

found2=(true false true true);
like image 686
woradin Avatar asked Sep 20 '25 10:09

woradin


1 Answers

You can use the [[ ]] operator. Here is a function based solution:

check_true() {
    [[ "${*}" =~ ^(true )*true$ ]]
    return
}

You can use it in this way:

$ found1=(true true true true)
$ found2=(true false true true)
$ check_true ${found1[*]} && echo OK
OK
$ check_true ${found2[*]} && echo OK

OK will be displayed as a result if the condition satisfies

like image 167
whoan Avatar answered Sep 23 '25 01:09

whoan