test
PURPOSE
Evaluates conditional expressions.
SYNOPSIS
test expr
[ expr ]
DESCRIPTION
The test command evaluates expression and, if its value
is true, returns a zero (true) exit value; otherwise it
returns a nonzero (false) exit value; test also returns a
nonzero exit value if there are no parameters.
Note: In the second form of the command, that is the one
that uses square brackets ("[ ]"), rather than the word
test, the brackets must be surrounded by blanks.
FUNCTIONS
All the functions and operators are separate parameters
to test. The following functions are used to construct
expression:
-r file True if file exists and has read permis-
sion.
-w file True if file exists and has write permis-
sion.
-x file True if file exists and has execute per-
mission.
-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.
-t [filedescr] True if the open file with file
descriptor number filedescr (1 by
default) is associated with a work
station device.
-z s1 True if the length of string s1 is zero.
-n s1 True if the length of the string s1 is
nonzero.
s1 = s2 True if strings s1 and s2 are identical.
s1 != s2 True if strings s1 and s2 are not iden-
tical.
s1 True if s1 is not the null string.
n1 -eq n2 True if the integers n1 and n2 are alge-
braically equal. Any of the comparisons
-ne, -gt, -ge, -lt, and -le can be used
in place of -eq.
These functions can be combined with the following opera-
tors:
! Unary negation operator.
-a Binary AND operator.
-o Binary OR operator (-a has higher preced-
ence than -o).
\( expression \)
Parentheses for grouping.
EXAMPLES
1. To test whether a file exists and is not empty:
if test ! -s "$1"
then
echo $1 does not exist or is empty.
fi
If the file specified by the first positional param-
eter to the shell procedure does not exist, this dis-
plays an error message. If "$1" exists, it displays
nothing. Note that there must be a space between -s
and the file name.
The double quotes around "$1" ensure that the test
will work properly even if the value of "$1" is the
empty string. If the double quotes are omitted and
"$1" is the empty string, test displays the error
message "test: parameter expected".
2. To do a complex comparison:
if [ $# -lt 2 -o ! -s "$1" ]
then
exit
fi
If the shell procedure was given fewer than two posi-
tional parameters or the file specified by "$1" does
not exist, then this exits the shell procedure. The
special shell variable "$#" represents the number of
positional parameters entered on the command line
that started this shell procedure. For more details,
see "Shell Variables and Command-Line Substitutions."
RELATED INFORMATION
The following commands: "find" and "sh."