TEST(1P)
TEST(1P) โ POSIX Programmer's Manual ๐
PROLOG ๐ง
okay so bestie, real talk: 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 expression
SYNOPSIS ๐
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, 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. ๐
OPTIONS ๐
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. ๐
โจ OPERANDS โจ
okay bestie, listen up, because this part is the actual menu of things test can check ๐
First, the rule about how you feed it stuff:
The application shall ensure that all operators and elements of primaries are presented as separate arguments to the test utility.
Separate arguments, bestie. Not mushed together. That's the whole vibe. ๐ซถ
The following primaries can be used to construct expression:
-bpathname โ True if pathname resolves to an 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. ๐งฑ-cpathname โ 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. ๐ค-dpathname โ 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. ๐-epathname โ True if pathname resolves to an existing directory entry. False if pathname cannot be resolved. ๐-fpathname โ 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. ๐-gpathname โ 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. ๐-hpathname โ 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. ๐-Lpathname โ 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. ๐-nstring โ True if the length of string is non-zero; otherwise, false. ๐-ppathname โ 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. ๐ฐ-rpathname โ 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. ๐-Spathname โ 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. ๐งฆ-spathname โ 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. โ๏ธ-tfile_descriptor โ True if file descriptor number file_descriptor is open and is associated with a terminal. False if file_descriptor is not a valid file descriptor number, or if file descriptor number file_descriptor is not open, or if it is open but is not associated with a terminal. ๐ฅ๏ธ-upathname โ 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. ๐-wpathname โ 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. โ๏ธ-xpathname โ 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. ๐-zstring โ 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โ True if both expression1 and expression2 are true; otherwise, false. The-abinary primary is left associative. It has a higher precedence than-o. ๐คexpression1 -o expression2โ True if either expression1 or expression2 is true; otherwise, false. The-obinary primary is left associative. ๐ฒ
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. So yeah โ it peeks through the link, except for those two. ๐ฆ
These primaries can be combined with the following operators:
!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. ๐
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 (yes, it's vibes-based, sorry) ๐ง
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:
- 0 arguments:
- Exit false (1).
- 1 argument:
- Exit true (0) if
$1is not null; otherwise, exit false.
- Exit true (0) if
- 2 arguments:
- If
$1is'!', exit true if$2is null, false if$2is not null. - If
$1is a unary primary, exit true if the unary test is true, false if the unary test is false. - Otherwise, produce unspecified results.
- If
- 3 arguments:
- If
$2is a binary primary, perform the binary test of$1and$3. - If
$1is'!', negate the two-argument test of$2and$3. - If
$1is'('and$3is')', perform the unary test of$2. On systems that do not support the XSI option, the results are unspecified if$1is'('and$3is')'. - Otherwise, produce unspecified results.
- If
- 4 arguments:
- If
$1is'!', negate the three-argument test of$2,$3, and$4. - If
$1is'('and$4is')', perform the two-argument test of$2and$3. On systems that do not support the XSI option, the results are unspecified if$1is'('and$4is')'. - Otherwise, the results are unspecified.
- If
- >4 arguments:
- The results are unspecified.
bestie. once you go past 4 arguments, all bets are OFF. results are unspecified. no cap. ๐
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. it does not want your input, bestie. ๐ซ
๐ธ INPUT FILES ๐ธ
None. nothing. nada. ๐
๐ธ ENVIRONMENT VARIABLES ๐ธ
okay so bestie, these are the environment variables that shall affect how test runs. lowkey important ๐
LANGโ gives you a default value for all the internationalization variables that are unset or null. (if you wanna know the precedence order of the i18n variables, go peek at the Base Definitions volume of POSIX.1-2017, Section 8.2, Internationalization Variables ๐)LC_ALLโ if you set this to a non-empty string value, it overrides every single one of the other internationalization variables. she's the main character, fr fr.LC_CTYPEโ decides the locale used for interpreting sequences of bytes of text data as characters (like, single-byte vs multi-byte characters in arguments, iykyk) ๐ฆLC_MESSAGESโ decides the locale that affects the format and contents of diagnostic messages written to standard error.NLSPATHโ decides where the message catalogs live, for the processing ofLC_MESSAGES. โจ
๐ ASYNCHRONOUS EVENTS ๐
Default. yeah. that's it. nothing dramatic happening here ๐
๐ STDOUT ๐
Not used. literally not a single byte. she's quiet, she's mysterious, she does not speak here. โจ
๐ STDERR ๐
The standard error shall be used only for diagnostic messages. that's her whole job, bestie.
๐ฆ OUTPUT FILES ๐ฆ
None. no files. zero. nada. ๐
โญ EXTENDED DESCRIPTION โญ
None. we love a low-effort section, periodt.
๐ EXIT STATUS ๐
The following exit values shall be returned:
0โ the expression evaluated to true. slay!1โ the expression evaluated to false, or the expression was missing. ๐ญ>1โ an error occurred. ๐
๐ CONSEQUENCES OF ERRORS ๐
Default.
heads up bestie โ everything from here on out is informative. it's the extra context, the lore, the commentary. still read it tho, it's important lore. ๐ซถ
๐ธ APPLICATION USAGE ๐ธ
okay so this is the part where we talk about the messy exes. the XSI extensions that specify the -a and -o binary primaries plus the '(' and ')' operators? yeah, they've been marked obsolescent. ๐ฌ (many expressions using them are ambiguously defined by the grammar depending on which specific expressions you're evaluating. it's giving chaos.)
Scripts using these expressions should be converted to the forms below. even though lots of implementations will keep supporting these obsolescent forms, your scripts should be extremely careful when dealing with user-supplied input that could get confused with these and other primaries and operators. like, unless the application developer knows every single case that produces input to the script, an invocation like this:
test "$1" -a "$2"
should be written as:
test "$1" && test "$2"
...to dodge problems when a user supplies values like $1 set to '!' and $2 set to the null string. so basically, when maximal portability is the concern, replace:
test expr1 -a expr2
with:
test expr1 && test expr2
and replace:
test expr1 -o expr2
with:
test expr1 || test expr2
but note: in test, -a has higher precedence than -o, while "&&" and "||" have equal precedence in the shell. so they're not a perfect swap, bestie, don't let anyone tell you otherwise. ๐
Parentheses or braces can be used in the shell command language to do 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 pop off if such a string expression got 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 aren't 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 get 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. ๐
โจ EXAMPLES โจ
okay bestie, real-world glow-ups, let's go ๐ซถ
- 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
- Perform a
mkdirif a directory does not exist:
test ! -d tempdir && mkdir tempdir
- Wait for a file to become non-readable:
while test -r thefile
do
sleep 30
done
echo '"thefile" is no longer readable'
- 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 (aka the lore, bestie) ๐ญ
So back in an early proposal, the KornShell-derived conditional command (double bracket [[]]) got removed from the shell command language description. And the objections were likeโฆ the real problem here is people misusing the test command ([), and shoving it into the shell is just the wrong way to fix that. Instead, proper documentation plus a new shell reserved word (!) are sufficient. Periodt.
Tests that need multiple test operations can totally 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. Not me trusting -oโฆ couldn't be me. ๐
XSI-conformant systems support more than four arguments. Okay slay.
XSI-conformant systems also support the combining of primaries with these constructs:
expression1 -a expression2โ True if bothexpression1andexpression2are true. ๐expression1 -o expression2โ True if at least one ofexpression1andexpression2are true. โจ( expression )โ True ifexpressionis true. ๐
And in evaluating these more complex combined expressions, the following precedence rules are used:
- 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-aoperator, and the-aoperator has higher precedence than the-ooperator. - The
-aand-ooperators are left associative. - The parentheses can be used to alter the normal precedence and associativity. ๐
Now, the BSD and System V versions of -f are not the same, bestie, they're giving different vibes entirely. The BSD definition was:
-f fileโ True iffileexists and is not a directory.
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). Consistency ate, honestly. ๐
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. Iconic behaviour fr fr. And 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. Like yeah, fair, we would absolutely confuse those. ๐
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:
-k fileโ True iffileexists and its sticky bit is set.-C fileโ True iffileis a contiguous file.-V fileโ True iffileis a version file.
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):
-l stringโ The length of the stringstring.
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. The audacity of that, honestly. ๐ญ
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. Gone but not forgotten ๐ซถ
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. Sloppy but iconic of them.
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. Tiny bracket, huge main character energy. ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
Likeโฆ none. Nothing. The future is now, bestie. โจ
๐ SEE ALSO ๐
- 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
Go read those, they're lowkey important iykyk ๐
๐ COPYRIGHT ๐
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 .
And that's a wrap on TEST(1P), bestie!! ๐ This page came to you from IEEE/The Open Group, 2017 โ the TEST(1P) era, the moment, the blueprint. Go forth and test responsibly. Mwah. ๐
โจ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms