test(1) test(1)
NAME [Toc] [Back]
test - condition evaluation command
SYNOPSIS [Toc] [Back]
test expr
[ expr ]
DESCRIPTION [Toc] [Back]
The test command evaluates the expression expr and, if its value is
True, returns a zero (true) exit status; otherwise, a nonzero (false)
exit status is returned. test also returns a nonzero exit status if
there are no arguments. The following primitives are used to
construct expr:
-r file True if file exists and is readable.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-f file True if file exists and is a regular file.
-d file True if file exists and is a directory.
-c file True if file exists and is a character special
file.
-b file True if file exists and is a block special file.
-p file True if file exists and is a named pipe (fifo).
-u file True if file exists and its set-user-ID bit is
set.
-g file True if file exists and its set-group-ID bit is
set.
-k file True if file exists and its sticky bit is set.
-s file True if file exists and has a size greater than
zero.
-h file True if file exists and is a symbolic link.
-t [fildes] True if the open file whose file descriptor number
is fildes (1 by default) is associated with a
terminal device.
-z s1 True if the length of string s1 is zero.
Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003
test(1) test(1)
-n s1 True if the length of the string s1 is non-zero.
s1 = s2 True if strings s1 and s2 are identical.
s1 != s2 True if strings s1 and s2 are not identical.
s1 True if s1 is not the null string.
n1 -eq n2 True if the integers n1 and n2 are algebraically
equal. Any of the comparisons -ne, -gt, -ge, -lt,
and -le can be used in place of -eq.
These primaries can be combined with the following operators:
! Unary negation operator.
-a Binary AND operator.
-o Binary OR operator (-a has higher precedence than
-o).
( expr ) Parentheses for grouping.
Note that all the operators and flags are separate arguments to test.
Note also that parentheses are significant to the shell and therefore
must be escaped. All file test operators return success if the
argument is a symbolic link that points to a file of the file type
being tested.
test is interpreted directly by the shell, and therefore does not
exist as a separate executable program.
EXTERNAL INFLUENCES [Toc] [Back]
International Code Set Support
Single byte and multibyte character code sets are supported.
EXAMPLES [Toc] [Back]
Exit if there are not two or three arguments:
if [ $# -l2 2 -o $# -gt 3 ]; then exit 1; fi
Create a new file containing the text string default if the file does
not already exist:
[ ! -f thisfile ] && echo default > thisfile
Wait for myfile to become non-readable:
while test -r myfile
do
sleep 30
Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003
test(1) test(1)
done
echo '"myfile" is no longer readable'
WARNINGS [Toc] [Back]
When the [ form of this command is used, the matching ] must be the
final argument, and both must be separate arguments from the arguments
they enclose (white space delimiters required.
Parentheses and other special shell metacharacters intended to be
handled by test must be escaped or quoted when invoking test from a
shell.
Avoid such problems when comparing strings by inserting a non-operator
character at the beginning of both operands:
test "X$response" = "Xexpected string"
This approach does not work with numeric comparisons or the unary
operators because it would affect the operand being checked.
AUTHOR [Toc] [Back]
test was developed by the University of California, Berkeley and HP.
SEE ALSO [Toc] [Back]
find(1), sh-posix(1), sh(1).
STANDARDS CONFORMANCE [Toc] [Back]
test: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
[: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
Hewlett-Packard Company - 3 - HP-UX 11i Version 2: August 2003 [ Back ] |