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.
When checking file, one should consider TOCTOU race-conditions. Avoiding the issue altogether with directly using file instead is strongly recommended.
The following primaries are used to construct expression:
-b
file-c
file-d
file-e
file-ef
file2-f
file-g
file-h
file-L
instead.-L
file-k
file-n
string-nt
file2-ot
file2-p
file-r
file-S
file-s
file-t
file_descriptor-u
file-w
file-x
file-z
string-O
file-G
file=
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.
test
should be compliant with the IEEE Std
1003.1-2024 (“POSIX.1”) specification.
The primaries ==
,
-G
, and -O
are extensions.
The -a
, -o
binary primaries,
and ‘(
’ and
‘)
’ operators are kept for backward
compatibility but should be taken as deprecated.
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 | Linux 6.6.67-gentoo-x86_64 |