TEST(1) | General Commands Manual | TEST(1) |
test
, [
—
condition evaluation utility
test |
expression |
[ |
expression ] |
The test
utility evaluates the expression
and, if it evaluates to true, returns a zero (true) exit status; otherwise
it returns 1 (false). If there is no expression,
test
also returns 1 (false).
All operators and flags are separate arguments to the
test
utility.
The following primaries are used to construct expression:
-b
file-c
file-d
file-e
file-f
file-g
file-h
file-L
instead.-k
file-n
string-p
file-r
file-s
file-t
file_descriptor-u
file-w
file-x
file-z
string-L
file-O
file-G
file-S
file-nt
file2-ot
file2-ef
file2=
s2!=
s2<
s2>
s2-eq
n2-ne
n2-gt
n2-ge
n2-lt
n2-le
n2If file is a symbolic link,
test
will fully dereference it and then evaluate the
expression against the file referenced, except for the
-h
and -L
primaries.
These primaries can be combined with the following operators:
!
expression-a
expression2-o
expression2(
expression )
The -a
operator has higher precedence than
the -o
operator.
Some shells may provide a builtin test
command which is similar or identical to this utility. Consult the
builtin(1) manual page.
The test
grammar is inherently ambiguous.
In order to assure a degree of consistency, the cases described in the
IEEE Std 1003.2 (“POSIX.2”), section
D11.2/4.62.4, standard are evaluated consistently according to the rules
specified in the standards document. All other cases are subject to the
ambiguity in the command semantics.
In particular, only expressions containing
-a
, -o
,
(
or )
can be ambiguous.
The test
utility exits with one of the
following values:
Implement test FILE1 -nt FILE2
using only
POSIX functionality:
test -n "$(find -L -- FILE1
-prune -newer FILE2 2>/dev/null)"
This can be modified using non-standard
find(1) primaries like
-newerca
to compare other timestamps.
For compatibility with some other implementations, the
=
primary can be substituted with
==
with the same meaning.
The test
utility implements a superset of
the IEEE Std 1003.2 (“POSIX.2”)
specification. The primaries <
,
==
, >
,
-ef
, -nt
,
-ot
, -G
, and
-O
are extensions.
A test
utility appeared in
Version 7 AT&T UNIX.
Both sides are always evaluated in -a
and
-o
. For instance, the writable status of
file will be tested by the following command even
though the former expression indicated false, which results in a gratuitous
access to the file system:
[ -z abc -a -w file ]
[ -z abc ] && [ -w file
]
October 5, 2016 | macOS 15.2 |