test - Test for a condition
Format
test expression [ expression ]
Description
test checks for various properties of files, strings, and integers. It does not produce any output other than error messages, but returns the result of the test as the exit status.
The second form of the test command [ expression ] is synonymous with the first. For more information about [[ ... ]], see rewoco in the ssh command description.
The following is a list of recognized operands:
The command line is a Boolean expression. The simplest expression is a string that is true if the string is nonempty (that is, has nonzero length). More complex expressions are composed of operators and operands, each of which is a separate argument (that is, surrounded by white space). The operators imply the number and type of their operands. The operators taking a file operand evaluate as false (without error) if the file does not exist.
The following is a list of recognized operands:
- -Aa file
- True if file has an extended access ACL entry.
- -Ad file
- True if file is a directory with a directory default ACL.
- -Af file
- True if file is a directory with a file default ACL.
- -b file
- True if file is a block special file (block special files are not supported)
- -B file
- True if the file is tagged as binary (not text)
- -c file
- True if file is a character special file
- -d file
- True if file is a directory
- -e file
- True if file exists
- -Ea file
- True if the file has the APF extended attribute
- -Ep file
- True if the file has the program control extended attribute
- -Es file
- True if the file has the shared address space extended attribute
- -El file
- True if the file has the shared library extended attribute
- -f file
- True if file is an ordinary file
- -g file
- True if the set-group-ID attribute of file is on
- -h file
- True if file is a symbolic link
- -k file
- True if the sticky bit is on file is on
- -L file
- True if file is a symbolic link
- -Ma file
- True if the file has any multilevel security label.
- -n string
- True if the length of string is greater than zero
- -p file
- True if file is a FIFO (named pipe)
- -r file
- True if file is readable (based on the security product's check against the effective user/group)
- -s file
- True if size of the file is nonzero
- -f fd
- True if the numeric file descriptor fd is open and associated with a terminal
- -T file
- True if the file is tagged as text
- -u file
- True if the set-user-ID attribute of file is on
- -w file
- True if file is writable (based on the security product's check against the effective user/group)
- -x file
- True if file is executable (based on the security product's check against the effective user/group)
- -z string
- True if the length of the string is zero
- string
- True if string is not a null string
- string1 = string2
- True if string1 and string2 are identical
- string != string
- True if string1 and string2 are not identical
- number1 -eq number2
- True if number1 and number2 are equal
Within the shell, either number can be an arbitrary shell arithmetic expression; the same applies for the other five numerical comparisons that follow. Both number1 and number2 must be integers.
- number1 -ge number2
- True if number1 is greater than or equal to number2
- number1 -gt number2
- True if number1 is greater than number2
- number1 -le number2
- True if number1 is less than or equal to number2
- number1 -lt number2
- True if number1 is less than number2
- number1 -ne number2
- True if number1 is not equal to number2
- file1 -nt file2
- True if file1 is newer than file2
- file1 -ot file2
- True if file1 is older than file2
- file1 -ef file2
- True if file1 has the same device and inode number as file2
- file -CS codeset
- True if the file is tagged with the codeset
- file -Ml seclabel
- True if the file has the multilevel security label seclabel. False if the file does not have a security label that matches seclabel
- expr1 -a expr2
- Logical AND; true if both expr1 and expr2 are true
- expr1 -o expr2
- Logical OR; true if either expr1 and expr2 is true
- ! expr
- Logical negation; true if expr is false
- ( expr )
- Binding; true if expr is true
The precedence of the operators in descending order is: unary operators, comparison operators, logical AND, logical OR.
[ expression ]
is synonymous with the first.Usage notes
- test is a built-in shell command.
- test can compare variables; however, if the variable is null, the
expression might be incorrect for test. For example:
does not work, because the z/OS shell expands this to:NULL= test $NULL = "so"
which is not a valid expression for test. A way to get around this is to prepend some value to both strings, as in:test = "so"
Failure to quote variable expansions is a common mistake. For example:test x$NULL = x"so"
Iftest $NULL != string
NULL
is undefined or empty, this results in:
which is not a valid test expression. This problem can be fixed by enclosing $NULL in quotes.test != string
These two examples perform basically the same function; that is, they protect the command against a variable having a possible null value.
Examples
if [ -f $1 ]
then
echo $1 is a file
elif [ -d $1 ]
then
echo $1 is a directory
else
echo $1 neither file nor directory
fi
This example illustrates the use of test,
and is not intended to be an efficient method.Localization
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_SYNTAX
- NLSPATH
Exit values
0
- The expression was true
1
- The expression was false
2
- The expression was badly formed
Portability
POSIX.2, X/Open Portability Guide, UNIX systems.
The -k, -L, -nt, -ot, -ef, -a, and -o operators plus the use of parentheses to group operators together are all extensions of the POSIX standard.
Related information
expr, find, let, ls, sh