test, [ - Evaluates conditional expressions
Interfaces documented on this reference page conform to industry standards as follows:
test: XPG4, XPG4-UNIX
[: XPG4, XPG4-UNIX
Refer to the
reference page for more information
about industry standards and associated tags.
The expression to be evaluated as true or false. This expression
is constructed from the operators and elements explained in the
The test command evaluates an expression constructed of functions and operators. If the value of expression is true, test returns an exit value of zero (0); otherwise, it returns FALSE, a nonzero exit value. The test command also returns a nonzero exit value if there are no arguments.
The alternate form of the command surrounds
with brackets ([ ]). When you use this
form, you must surround the brackets with spaces.
All of the listed functions and operators are separate arguments to test.
The following functions are used to construct expression: TRUE if file exists and has read permission. TRUE if file exists and has write permission. TRUE if file exists and has execute permission. TRUE if file exists and is a regular file. TRUE if file exists and is a directory. TRUE if file exists. TRUE if file exists and is a character-special file. TRUE if file exists and is a block-special file. TRUE if file exists and is a named pipe (FIFO). [Compaq] TRUE if file exists and is a soft link. Synonym for -L expression. TRUE if file exists and is a soft link. Synonym for -h expression. TRUE if file exists and its set-user ID bit is set. TRUE if file exists and its set-group ID bit is set. [Compaq] TRUE if file exists and its sticky bit is set. TRUE if file exists and has a size greater than zero (0). TRUE if the open file with file descriptor number file_descriptor (1 by default) is associated with a terminal device. TRUE if the length of string1 is zero (0). TRUE if the length of string1 is nonzero. TRUE if string1 and string2 are identical. TRUE if string1 and string2 are not identical. TRUE if string1 is not the null string. TRUE if the integers number1 and number2 are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq.
The listed functions can be combined with the following operators:
Unary negation operator.
Binary AND operator.
Binary OR operator (the
has higher precedence than the
[Compaq] Parentheses for grouping.
The following exit values are returned:
and its value is TRUE.
and is value is FALSE, or there are no arguments.
An error occurred.
To test whether a file exists and is not empty, enter:
if test ! -s "$1"
echo $1 does not exist or is empty.
The following environment variables affect the execution of
Provides a default value for the internationalization variables
that are unset or null. If
is unset or null, the corresponding value from the default locale is used.
If any of the internationalization variables contain an invalid setting, the
utility behaves as if none of the variables had been defined.
If set to a non-empty string value, overrides the values of
all the other internationalization variables.
Determines the locale for the interpretation of sequences
of bytes of text data as characters (for example, single-byte as opposed to
multibyte characters in arguments).
Determines the locale for the format and contents of diagnostic
messages written to standard error.
Determines the location of message catalogues for the processing
Commands: csh(1), find(1), ksh(1), Bourne shell sh(1b), POSIX shell sh(1p)