test (1p)
PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.NAME
test — evaluate expressionSYNOPSIS
test [expression]
[ [expression] ]
DESCRIPTION
The test utility shall evaluate the expression and indicate the result of the evaluation by its exit status. An exit status of zero indicates that the expression evaluated as true and an exit status of 1 indicates that the expression evaluated as false. In the second form of the utility, which uses "[]" rather than test, the application shall ensure that the square brackets are separate arguments.OPTIONS
The test utility shall not recognize the "−−" argument in the manner specified by Guideline 10 in the Base Definitions volume of POSIX.1‐2008, Section 12.2, Utility Syntax Guidelines. No options shall be supported.OPERANDS
The application shall ensure that all operators and elements of primaries are presented as separate arguments to the test utility. The following primaries can be used to construct expression:- −b pathname
- True if pathname resolves to en existing directory entry for a block special file. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a block special file.
- −c pathname
- True if pathname resolves to an existing directory entry for a character special file. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a character special file.
- −d pathname
- True if pathname resolves to an existing directory entry for a directory. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a directory.
- −e pathname
- True if pathname resolves to an existing directory entry. False if pathname cannot be resolved.
- −f pathname
- True if pathname resolves to an existing directory entry for a regular file. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a regular file.
- −g pathname
- True if pathname resolves to an existing directory entry for a file that has its set-group-ID flag set. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that does not have its set-group-ID flag set.
- −h pathname
- True if pathname resolves to an existing directory entry for a symbolic link. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a symbolic link. If the final component of pathname is a symbolic link, that symbolic link is not followed.
- −L pathname
- True if pathname resolves to an existing directory entry for a symbolic link. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a symbolic link. If the final component of pathname is a symbolic link, that symbolic link is not followed.
- −n string
- True if the length of string is non-zero; otherwise, false.
- −p pathname
- True if pathname resolves to an existing directory entry for a FIFO. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a FIFO.
- −r pathname
- True if pathname resolves to an existing directory entry for a file for which permission to read from the file will be granted, as defined in Section 1.1.1.4, File Read, Write, and Creation. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file for which permission to read from the file will not be granted.
- −S pathname
- True if pathname resolves to an existing directory entry for a socket. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a socket.
- −s pathname
- True if pathname resolves to an existing directory entry for a file that has a size greater than zero. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that does not have a size greater than zero.
- −t file_descriptor
-
- −u pathname
- True if pathname resolves to an existing directory entry for a file that has its set-user-ID flag set. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that does not have its set-user-ID flag set.
- −w pathname
- True if pathname resolves to an existing directory entry for a file for which permission to write to the file will be granted, as defined in Section 1.1.1.4, File Read, Write, and Creation. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file for which permission to write to the file will not be granted.
- −x pathname
- True if pathname resolves to an existing directory entry for a file for which permission to execute the file (or search it, if it is a directory) will be granted, as defined in Section 1.1.1.4, File Read, Write, and Creation. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file for which permission to execute (or search) the file will not be granted.
- −z string
- True if the length of string string is zero; otherwise, false.
- string
- True if the string string is not the null string; otherwise, false.
- s1 = s2
- True if the strings s1 and s2 are identical; otherwise, false.
- s1 != s2
- True if the strings s1 and s2 are not identical; otherwise, false.
- n1 −eq n2
- True if the integers n1 and n2 are algebraically equal; otherwise, false.
- n1 −ne n2
- True if the integers n1 and n2 are not algebraically equal; otherwise, false.
- n1 −gt n2
- True if the integer n1 is algebraically greater than the integer n2; otherwise, false.
- n1 −ge n2
- True if the integer n1 is algebraically greater than or equal to the integer n2; otherwise, false.
- n1 −lt n2
- True if the integer n1 is algebraically less than the integer n2; otherwise, false.
- n1 −le n2
- True if the integer n1 is algebraically less than or equal to the integer n2; otherwise, false.
- expression1 −a expression2
-
- expression1 −o expression2
-
- ! expression
- True if expression is false. False if expression is true.
- ( expression )
- True if expression is true. False if expression is false. The parentheses can be used to alter the normal precedence and associativity.
−primary_operator primary_operand
primary_operand −primary_operator primary_operand
primary_operand primary_operator primary_operand
- 0 arguments:
- Exit false (1).
- 1 argument:
- Exit true (0) if $1 is not null; otherwise, exit false.
- 2 arguments:
-
- *
- If $1 is '!', exit true if $2 is null, false if $2 is not null.
- *
- If $1 is a unary primary, exit true if the unary test is true, false if the unary test is false.
- *
- Otherwise, produce unspecified results.
- 3 arguments:
-
- *
- If $2 is a binary primary, perform the binary test of $1 and $3.
- *
- If $1 is '!', negate the two-argument test of $2 and $3.
- *
- If $1 is '(' and $3 is ')', perform the unary test of $2. On systems that do not support the XSI option, the results are unspecified if $1 is '(' and $3 is ')'.
- *
- Otherwise, produce unspecified results.
- 4 arguments:
-
- *
- If $1 is '!', negate the three-argument test of $2, $3, and $4.
- *
- If $1 is '(' and $4 is ')', perform the two-argument test of $2 and $3. On systems that do not support the XSI option, the results are unspecified if $1 is '(' and $4 is ')'.
- *
- Otherwise, the results are unspecified.
- >4 arguments:
- The results are unspecified.
On XSI-conformant systems, combinations of primaries and operators shall be evaluated using the precedence and associativity rules described previously. In addition, the string comparison binary primaries '=' and "!=" shall have a higher precedence than any unary primary.
STDIN
Not used.INPUT FILES
None.ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of test:- LANG
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1‐2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- LC_ALL
- If set to a non-empty string value, override the values of all the other internationalization variables.
- LC_CTYPE
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments).
- LC_MESSAGES
-
- NLSPATH
- Determine the location of message catalogs for the processing of LC_MESSAGES.
ASYNCHRONOUS EVENTS
Default.STDOUT
Not used.STDERR
The standard error shall be used only for diagnostic messages.OUTPUT FILES
None.EXTENDED DESCRIPTION
None.EXIT STATUS
The following exit values shall be returned:- 0
- expression evaluated to true.
- 1
- expression evaluated to false or expression was missing.
- >1
- An error occurred.
CONSEQUENCES OF ERRORS
Default. The following sections are informative.APPLICATION USAGE
The XSI extensions specifying the −a and −o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:test "$1" −a "$2"
test "$1" && test "$2"
test expr1 −a expr2
test expr1 && test expr2
test expr1 −o expr2
test expr1 || test expr2
test \( expr1 −a expr2 \) −o expr3
( test expr1 && test expr2 ) || test expr3
test "$1" test ! "$1"
test −n "$1" test −z "$1"
test "$response" = "expected string"
test "X$response" = "Xexpected string" test "expected string" = "$response"
test −d $1 −o −d $2
test \( −d "$1" \) −o \( −d "$2" \) test −d "$1" || test −d "$2"
test "$1" = "bat" −a "$2" = "ball"
test "X$1" = "Xbat" −a "X$2" = "Xball" test "$1" = "bat" && test "$2" = "ball" test "X$1" = "Xbat" && test "X$2" = "Xball"
EXAMPLES
- 1.
- Exit if there are not two or three arguments (two variations):
if [ $# −ne 2 ] && [ $# −ne 3 ]; then exit 1; fi if [ $# −lt 2 ] || [ $# −gt 3 ]; then exit 1; fi
- 2.
- Perform a mkdir if a directory does not exist:
test ! −d tempdir && mkdir tempdir
- 3.
- Wait for a file to become non-readable:
while test −r thefile do sleep 30 done echo '"thefile" is no longer readable'
- 4.
- Perform a command if the argument is one of three strings (two variations):
if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ] then command fi
case "$1" in pear|grape|apple) command ;; esac
RATIONALE
The KornShell-derived conditional command (double bracket [[]]) was removed from the shell command language description in an early proposal. Objections were raised that the real problem is misuse of the test command ([), and putting it into the shell is the wrong way to fix the problem. Instead, proper documentation and a new shell reserved word (!) are sufficient. Tests that require multiple test operations can be done at the shell level using individual invocations of the test command and shell logicals, rather than using the error-prone −o flag of test. XSI-conformant systems support more than four arguments. XSI-conformant systems support the combining of primaries with the following constructs:- expression1 −a expression2
-
- expression1 −o expression2
-
- ( expression )
-
- *
- The unary primaries have higher precedence than the algebraic binary primaries.
- *
- The unary primaries have lower precedence than the string binary primaries.
- *
- The unary and binary primaries have higher precedence than the unary string primary.
- *
- The ! operator has higher precedence than the −a operator, and the −a operator has higher precedence than the −o operator.
- *
- The −a and −o operators are left associative.
- *
- The parentheses can be used to alter the normal precedence and associativity.
- −f file
- True if file exists and is not a directory.
test −b foo −o −c foo −o −d foo −o −f foo −o −p foo
test −f foo −o −d foo
- −k file
- True if file exists and its sticky bit is set.
- −C file
- True if file is a contiguous file.
- −V file
- True if file is a version file.
- −l string
- The length of the string string.