2.13 Combining Primaries With Operators
Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:
( expr )Force precedence. True if expr is true.
-
! expr -not exprTrue if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it. The ‘-not’ operator does not exist in POSIX, so ‘!’ is an equivalent and more portable choice.
-
expr1 expr2 expr1 -a expr2expr1 -and expr2And; expr2 is not evaluated if expr1 is false. The ‘-and’ operator does not exist in POSIX, so ‘-a’ is an equivalent and more portable choice.
-
expr1 -o expr2 expr1 -or expr2Or; expr2 is not evaluated if expr1 is true. The ‘-or’ operator does not exist in POSIX, so ‘-o’ is an equivalent and more portable choice.
expr1 , expr2List; both expr1 and expr2 are always evaluated. True if expr2 is true. The value of expr1 is discarded. This operator lets you do multiple independent operations on one traversal, without depending on whether other operations succeeded. The two operations expr1 and expr2 are not always fully independent, since expr1 might have side effects like touching or deleting files, or it might use ‘-prune’ which would also affect expr2.
Please note that ‘-a’ when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly has higher precedence than ‘-o’. This means that ‘find afile bfile -name afile -o -name bfile -print’ will never print ‘afile’ .
find searches the directory tree rooted at each file name by
evaluating the expression from left to right, according to the rules
of precedence, until the outcome is known (the left hand side is false
for ‘-and’, true for ‘-or’), at which point find
moves on to the next file name.
There are two other tests that can be useful in complex expressions:
- Test: -true
Always true.
- Test: -false
Always false.