TEST(1POSIX) | POSIX Programmer's Manual | TEST(1POSIX) |
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.
test — evaluate expression
test [expression]
[ [expression] ]
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, where the utility name used is [ rather than test, the application shall ensure that the closing square bracket is a separate argument. The test and [ utilities may be implemented as a single linked utility which examines the basename of the zeroth command line argument to determine whether to behave as the test or [ variant. Applications using the exec() family of functions to execute these utilities shall ensure that the argument passed in arg0 or argv[0] is '[' when executing the [ utility and has a basename of "test" when executing the test utility.
The test utility shall not recognize the "--" argument in the manner specified by Guideline 10 in the Base Definitions volume of POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines.
No options shall be supported.
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:
With the exception of the -h pathname and -L pathname primaries, if a pathname argument is a symbolic link, test shall evaluate the expression by resolving the symbolic link and using the file referenced by the link.
These primaries can be combined with the following operators:
The primaries with two elements of the form:
-primary_operator primary_operand
are known as unary primaries. The primaries with three elements in either of the two forms:
primary_operand -primary_operator primary_operand
primary_operand primary_operator primary_operand
are known as binary primaries. Additional implementation-defined operators and primary_operators may be provided by implementations. They shall be of the form -operator where the first character of operator is not a digit.
The algorithm for determining the precedence of the operators and the return value that shall be generated is based on the number of arguments presented to test. (However, when using the "[...]" form, the <right-square-bracket> final argument shall not be counted in this algorithm.)
In the following list, $1, $2, $3, and $4 represent the arguments presented to test:
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.
Not used.
None.
The following environment variables shall affect the execution of test:
Default.
Not used.
The standard error shall be used only for diagnostic messages.
None.
None.
The following exit values shall be returned:
Default.
The following sections are informative.
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"
should be written as:
test "$1" && test "$2"
to avoid problems if a user supplied values such as $1 set to '!' and $2 set to the null string. That is, in cases where maximal portability is of concern, replace:
test expr1 -a expr2
with:
test expr1 && test expr2
and replace:
test expr1 -o expr2
with:
test expr1 || test expr2
but note that, in test, -a has higher precedence than -o while "&&" and "||" have equal precedence in the shell.
Parentheses or braces can be used in the shell command language to effect grouping.
Parentheses must be escaped when using sh; for example:
test \( expr1 -a expr2 \) -o expr3
This command is not always portable even on XSI-conformant systems depending on the expressions specified by expr1, expr2, and expr3. The following form can be used instead:
( test expr1 && test expr2 ) || test expr3
The two commands:
test "$1" test ! "$1"
could not be used reliably on some historical systems. Unexpected results would occur if such a string expression were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are:
test -n "$1" test -z "$1"
respectively.
Historical systems have also been unreliable given the common construct:
test "$response" = "expected string"
One of the following is a more reliable form:
test "X$response" = "Xexpected string" test "expected string" = "$response"
Note that the second form assumes that expected string could not be confused with any unary primary. If expected string starts with '-', '(', '!', or even '=', the first form should be used instead. Using the preceding rules without the XSI marked extensions, any of the three comparison forms is reliable, given any input. (However, note that the strings are quoted in all cases.)
Because the string comparison binary primaries, '=' and "!=", have a higher precedence than any unary primary in the greater than 4 argument case, unexpected results can occur if arguments are not properly prepared. For example, in:
test -d $1 -o -d $2
If $1 evaluates to a possible directory name of '=', the first three arguments are considered a string comparison, which shall cause a syntax error when the second -d is encountered. One of the following forms prevents this; the second is preferred:
test \( -d "$1" \) -o \( -d "$2" \) test -d "$1" || test -d "$2"
Also in the greater than 4 argument case:
test "$1" = "bat" -a "$2" = "ball"
syntax errors occur if $1 evaluates to '(' or '!'. One of the following forms prevents this; the third is preferred:
test "X$1" = "Xbat" -a "X$2" = "Xball" test "$1" = "bat" && test "$2" = "ball" test "X$1" = "Xbat" && test "X$2" = "Xball"
Note that none of the following examples are permitted by the syntax described:
[-f file] [-f file ] [ -f file] [ -f file test -f file ]
In the first two cases, if a utility named [‐f exists, that utility would be invoked, and not test. In the remaining cases, the brackets are mismatched, and the behavior is unspecified. However:
test ! ]
does have a defined meaning, and must exit with status 1. Similarly:
test ]
must exit with status 0.
if [ $# -ne 2 ] && [ $# -ne 3 ]; then exit 1; fi if [ $# -lt 2 ] || [ $# -gt 3 ]; then exit 1; fi
test ! -d tempdir && mkdir tempdir
while test -r thefile do sleep 30 done echo '"thefile" is no longer readable'
if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ] then command fi
case "$1" in pear|grape|apple) command ;; esac
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:
In evaluating these more complex combined expressions, the following precedence rules are used:
The BSD and System V versions of -f are not the same. The BSD definition was:
The SVID version (true if the file exists and is a regular file) was chosen for this volume of POSIX.1‐2017 because its use is consistent with the -b, -c, -d, and -p operands (file exists and is a specific file type).
The -e primary, possessing similar functionality to that provided by the C shell, was added because it provides the only way for a shell script to find out if a file exists without trying to open the file. Since implementations are allowed to add additional file types, a portable script cannot use:
test -b foo -o -c foo -o -d foo -o -f foo -o -p foo
to find out if foo is an existing file. On historical BSD systems, the existence of a file could be determined by:
test -f foo -o -d foo
but there was no easy way to determine that an existing file was a regular file. An early proposal used the KornShell -a primary (with the same meaning), but this was changed to -e because there were concerns about the high probability of humans confusing the -a primary with the -a binary operator.
The following options were not included in this volume of POSIX.1‐2017, although they are provided by some implementations. These operands should not be used by new implementations for other purposes:
The following option was not included because it was undocumented in most implementations, has been removed from some implementations (including System V), and the functionality is provided by the shell (see Section 2.6.2, Parameter Expansion.
The -b, -c, -g, -p, -u, and -x operands are derived from the SVID; historical BSD does not provide them. The -k operand is derived from System V; historical BSD does not provide it.
On historical BSD systems, test -w directory always returned false because test tried to open the directory for writing, which always fails.
Some additional primaries newly invented or from the KornShell appeared in an early proposal as part of the conditional command ([[]]): s1 > s2, s1 < s2, str = pattern, str != pattern, f1 -nt f2, f1 -ot f2, and f1 -ef f2. They were not carried forward into the test utility when the conditional command was removed from the shell because they have not been included in the test utility built into historical implementations of the sh utility.
The -t file_descriptor primary is shown with a mandatory argument because the grammar is ambiguous if it can be omitted. Historical implementations have allowed it to be omitted, providing a default of 1.
It is noted that '[' is not part of the portable filename character set; however, since it is required to be encoded by a single byte, and is part of the portable character set, the name of this utility forms a character string across all supported locales.
None.
Section 1.1.1.4, File Read, Write, and Creation, find
The Base Definitions volume of POSIX.1‐2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
2017 | IEEE/The Open Group |