Manual Page Result
0
Command: test | Section: 1 | Source: UNIX v10 | File: test.1
TEST(1) General Commands Manual TEST(1)
NAME
test, [, newer - condition commands
SYNOPSIS
test expr
[ expr ]
newer file1 file2
DESCRIPTION
Test evaluates the expression expr. If the value is true the exit sta-
tus is 0; otherwise the exit status is nonzero. If there are no argu-
ments the exit status is nonzero.
The following primitives are used to construct expr.
-r file True if the file exists (is accessible) and is readable.
-w file True if the file exists and is writable.
-x file True if the file exists and has execute permission.
-e file True if the file exists.
-f file True if the file exists and is a plain file.
-d file True if the file exists and is a directory.
-c file True if the file exists and is a character special file.
-b file True if the file exists and is a block special file.
-L file True if the file is a symbolic link.
-u file True if the file exists and has set userid permission.
-g file True if the file exists and has set groupid permission.
-s file True if the file exists and has a size greater than zero.
-t fildes True if the open file whose file descriptor number is fildes
(1 by default) is associated with a terminal device.
-S True if the effective userid is zero.
s1 = s2 True if the strings s1 and s2 are identical.
s1 != s2 True if the strings s1 and s2 are not identical.
s1 True if s1 is not the null string. (Deprecated.)
-z s1 True if the length of string s1 is zero.
n1 -eq n2 True if the integers n1 and n2 are arithmetically equal. Any
of the comparisons -ne, -gt, -ge, -lt, or -le may be used in
place of -eq. The (nonstandard) construct -l string, meaning
the length of string, may be used in place of an integer.
These primaries may be combined with the following operators:
! unary negation operator
-o binary or operator
-a binary and operator; higher precedence than -o
( expr )
parentheses for grouping.
Notice that all the operators and flags are separate arguments to test.
Notice also that parentheses are meaningful to the Shell and must be
escaped.
[ is a synonym for test, except that [ requires a closing ].
Newer returns a zero exit code if file1 exists and file2 does not, or
if file1 and file2 both exist and file1 was modified at least as re-
cently as file2. It returns a non-zero return code otherwise.
EXAMPLES
Test is a dubious way to check for specific character strings: it uses
a process to do what a shell case statement can do. The first example
is not only inefficient but wrong, because test understands the pur-
ported string "-c" as an option. Furthermore $1 might be empty.
if test $1 = "-c" # wrong!
then echo OK
fi
A correct way is
case "$1" in
-c) echo OK
esac
Test whether is in the current directory.
test -e abc -o -L abc
SEE ALSO
sh(1), find(1)
TEST(1)