A standard *n?x program, often hardcoded into the
shell because of
its frequent use.
test allows a shell programmer to test various conditions
that result in a boolean value. It is usually used in combination
with other shell components, such as if, (allowing parts
of a shell script to be executed conditionally) or while
(to perform the test on a loop).
test takes various arguments, interprets them as a large
boolean expression (see below), and sets its exit status code to 0
if the expression is true or some nonzero value if it is false.
test is aliased by every *nix system to "[",
which makes
if [ -f "$dest" ]
equivalent to
if test -f "$dest"
If test is invoked as "[", the last argument must be,
of course , "]".
The most basic expressions to
test are literal strings.
However, your shell can build complex string variables from
shell variables (or
command substitution)
and
test won't know the difference.
There are string comparison operators that take two expressions and
compare the strings resulting from them.
expr1 = expr2 equality
expr1 != expr2 inequality
expr1
is the string nonempty?
-z expr1 is
the string empty?
-n expr1 is
the string nonempty?
There are numeric comparison operators that take two expressions and
compare the numbers (integers only) resulting from them.
expr1 -eq expr2 equality
expr1 -ne expr2 inequality
expr1 -gt expr2 greater than
expr1 -lt expr2 less than
expr1 -ge expr2 greater than
or equal
expr1 -le expr2 less than
or equal
There is a unary negation operator
! expr
and boolean operators:
expr1 -a expr2 and
expr1 -o expr2 or
However, since operator precedence is an unheard-of concept (don't
believe the man page) to test,
you will always want to use parentheses (and since parentheses have their
own special interpretation within the shell, so you must always quote them):
'(' expr1 ')' -a '(' expr2 ')'
There are unary operators that test what your expressions mean to the
file system:
-f expr Is expr
the path to a regular file?
-d expr Is
expr
the path to a directory?
-l expr Is
expr
the path to a symbolic link (also -h and -L)?
-c expr Is
expr
the path to a character special file?
-b expr Is
expr
the path to a block special file?
-p expr Is
expr
the path to a named pipe?
-t [expr]
Is expr the file descriptor of an open shell
file that is also a terminal device?
(useful only for 0 (stdin), 1 (stdout),
and 2 (stderr). The default is 1.
-x expr Does
expr
exist in the file system?
-r expr Is
expr
readable?
-w expr Is
expr
writable?
-u expr Is expr
a file whose "set user ID" bit is set?
-g expr Is
expr
a file whose "set group ID" bit is set?
-k expr Is
expr
a file whose "sticky bit" is set?
-s expr Is
expr
a file whose size is greater than zero?