2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
3 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
4 .\" Copyright 1989 AT&T
5 .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
8 .\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
9 .\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions 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.
10 .\" This notice shall appear on any product containing this material.
11 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
12 .\" See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with
13 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
14 .TH TEST 1 "Aug 11, 2009"
16 test \- evaluate condition(s)
20 \fB/usr/bin/test\fR [\fIcondition\fR]
25 \fB[\fR [\fIcondition\fR] ]
31 \fBtest\fR [\fIcondition\fR]
36 \fB[\fR [\fIcondition\fR] ]
42 \fBtest\fR [\fIcondition\fR]
47 \fB[\fR [\fIcondition\fR] ]
53 \fBtest\fR [\fIcondition\fR]
58 \fB[\fR [\fIcondition\fR] ]
64 \fBtest\fR [\fIcondition\fR]
69 \fB[\fR [\fIcondition\fR] ]
75 The \fBtest\fR utility evaluates the \fIcondition\fR and indicates the result
76 of the evaluation by its exit status. An exit status of zero indicates that the
77 condition evaluated as true and an exit status of 1 indicates that the
78 condition evaluated as false.
81 In the first form of the utility shown using the SYNOPSIS:
85 test [\fIcondition\fR]
92 the square brackets denote that \fIcondition\fR is an optional operand and are
93 not to be entered on the command line.
96 In the second form of the utility shown using the SYNOPSIS:
100 \fB[\fR [ \fIcondition\fR ] \fB]\fR
107 the first open square bracket, \fB[\fR, is the required utility name.
108 \fIcondition\fR is optional, as denoted by the inner pair of square brackets.
109 The final close square bracket, \fB]\fR, is a required operand.
112 See \fBlargefile\fR(5) for the description of the behavior of \fBtest\fR when
113 encountering files greater than or equal to 2 Gbyte (2^31 bytes).
116 The \fBtest\fR and \fB[\fR utilities evaluate the condition \fIcondition\fR
117 and, if its value is true, set exit status to \fB0\fR. Otherwise, a non-zero
118 (false) exit status is set. \fBtest\fR and \fB[\fR also set a non-zero exit
119 status if there are no arguments. When permissions are tested, the effective
120 user \fBID\fR of the process is used.
123 All operators, flags, and brackets (brackets used as shown in the last SYNOPSIS
124 line) must be separate arguments to these commands. Normally these arguments
125 are separated by spaces.
129 The primaries listed below with two elements of the form:
133 \fI-primary_operator primary_operand\fR
139 are known as \fBunary primaries\fR. The primaries with three elements in either
144 \fIprimary_operand -primary_operator primary_operand
145 primary_operand primary_operator primary_operand\fR
151 are known as \fBbinary primaries\fR.
154 If any file operands except for \fB-h\fR and \fB-L\fR primaries refer to
155 symbolic links, the symbolic link is expanded and the test is performed on the
159 If you test a file you own (the \fB-r\fR \fB-w\fR or \fB-x\fR tests), but the
160 permission tested does not have the \fIowner\fR bit set, a non-zero (false)
161 exit status is returned even though the file can have the \fBgroup\fR or
162 \fBother\fR bit set for that permission.
165 The \fB=\fR and \fB!=\fR primaries have a higher precedence than the unary
166 primaries. The \fB=\fR and \fB!=\fR primaries always expect arguments;
167 therefore, \fB=\fR and \fB!=\fR cannot be used as an argument to the unary
171 The following primaries can be used to construct \fIcondition\fR:
175 \fB\fB-a\fR \fIfile\fR\fR
178 True if \fIfile\fR exists. (Not available in \fBsh\fR.)
184 \fB\fB-b\fR \fIfile\fR\fR
187 True if \fIfile\fR exists and is a block special file.
193 \fB\fB-c\fR \fIfile\fR\fR
196 True if \fIfile\fR exists and is a character special file.
202 \fB\fB-d\fR \fIfile\fR\fR
205 True if \fIfile\fR exists and is a directory.
211 \fB\fB-e\fR \fIfile\fR\fR
214 True if \fIfile\fR exists. (Not available in \fBsh\fR.)
220 \fB\fB-f\fR \fIfile\fR\fR
223 True if \fIfile\fR exists and is a regular file. The \fBcsh\fR \fBtest\fR
224 and \fB[\fR built-ins return true if \fIfile\fR exists and is
225 (\fBnot\(mia\(midirectory\fR).
231 \fB\fB-g\fR \fIfile\fR\fR
234 True if \fIfile\fR exists and its set group \fBID\fR flag is set.
240 \fB\fB-G\fR \fIfile\fR\fR
243 True if \fIfile\fR exists and its group matches the effective group \fBID\fR of
244 this process. (Not available in \fBsh\fR.)
250 \fB\fB-h\fR \fIfile\fR\fR
253 True if \fIfile\fR exists and is a symbolic link.
259 \fB\fB-k\fR \fIfile\fR\fR
262 True if \fIfile\fR exists and has its sticky bit set.
268 \fB\fB-L\fR \fIfile\fR\fR
271 True if \fIfile\fR exists and is a symbolic link.
277 \fB\fB-n\fR \fIstring\fR\fR
280 True if the length of \fIstring\fR is non-zero.
286 \fB\fB-o\fR \fIoption\fR\fR
289 True if option named \fIoption\fR is on. This option is not available in
290 \fBcsh\fR or \fBsh\fR.
296 \fB\fB-O\fR \fIfile\fR\fR
299 True if \fIfile\fR exists and is owned by the effective user \fBID\fR of this
300 process. This option is not available in \fBsh\fR.
306 \fB\fB-p\fR \fIfile\fR\fR
309 True if \fIfile\fR is a named pipe (\fBFIFO\fR).
315 \fB\fB-r\fR \fIfile\fR\fR
318 True if \fIfile\fR exists and is readable.
324 \fB\fB-s\fR \fIfile\fR\fR
327 True if \fIfile\fR exists and has a size greater than zero.
333 \fB\fB-S\fR \fIfile\fR\fR
336 True if \fIfile\fR exists and is a socket. This option is not available in
343 \fB\fB-t\fR [\fIfile_descriptor\fR]\fR
346 True if the file whose file descriptor number is \fIfile_descriptor\fR is open
347 and is associated with a terminal. If \fIfile_descriptor\fR is not specified,
348 \fB1\fR is used as a default value.
354 \fB\fB-u\fR \fIfile\fR\fR
357 True if \fIfile\fR exists and its set-user-ID flag is set.
363 \fB\fB-w\fR \fIfile\fR\fR
366 True if \fIfile\fR exists and is writable. True indicates only that the write
367 flag is on. The \fIfile\fR is not writable on a read-only file system even if
368 this test indicates true.
374 \fB\fB-x\fR \fIfile\fR\fR
377 True if \fIfile\fR exists and is executable. True indicates only that the
378 execute flag is on. If \fIfile\fR is a directory, true indicates that
379 \fIfile\fR can be searched.
385 \fB\fB-z\fR \fIstring\fR\fR
388 True if the length of string \fIstring\fR is zero.
394 \fB\fIfile1\fR \fB-nt\fR \fIfile2\fR\fR
397 True if \fIfile1\fR exists and is newer than \fIfile2\fR. This option is not
398 available in \fBsh\fR.
404 \fB\fIfile1\fR \fB-ot\fR \fIfile2\fR\fR
407 True if \fIfile1\fR exists and is older than \fIfile2\fR. This option is not
408 available in \fBsh\fR.
414 \fB\fIfile1\fR \fB-ef\fR \fIfile2\fR\fR
417 True if \fIfile1\fR and \fIfile2\fR exist and refer to the same file. This
418 option is not available in \fBsh\fR.
427 True if the string \fIstring\fR is not the null string.
433 \fB\fIstring1\fR \fB=\fR \fIstring2\fR\fR
436 True if the strings \fIstring1\fR and \fIstring2\fR are identical.
442 \fB\fIstring1\fR \fB!=\fR \fIstring2\fR\fR
445 True if the strings \fIstring1\fR and \fIstring2\fR are not identical.
451 \fB\fIn1\fR \fB-eq\fR \fIn2\fR\fR
454 True if the numbers \fIn1\fR and \fIn2\fR are algebraically equal. A number may
455 be integer, floating point or floating-point constant (such as [+/-]Inf,
456 [+/-]NaN) in any format specified by C99/XPG6/SUS.
462 \fB\fIn1\fR \fB-ne\fR \fIn2\fR\fR
465 True if the numbers \fIn1\fR and \fIn2\fR are not algebraically equal. A number
466 may be integer, floating point or floating-point constant (such as [+/-]Inf,
467 [+/-]NaN) in any format specified by C99/XPG6/SUS.
473 \fB\fIn1\fR \fB-gt\fR \fIn2\fR\fR
476 True if the number \fIn1\fR is algebraically greater than the number \fIn2\fR.
477 A number may be integer, floating point or floating-point constant (such as
478 [+/-]Inf, [+/-]NaN) in any format specified by C99/XPG6/SUS.
484 \fB\fIn1\fR \fB-ge\fR \fIn2\fR\fR
487 True if the number \fIn1\fR is algebraically greater than or equal to the
488 number \fIn2\fR. A number may be integer, floating point or floating-point
489 constant (such as [+/-]Inf, [+/-]NaN) in any format specified by C99/XPG6/SUS.
495 \fB\fIn1\fR \fB-lt\fR \fIn2\fR\fR
498 True if the number \fIn1\fR is algebraically less than the number \fIn2\fR. A
499 number may be integer, floating point or floating-point constant (such as
500 [+/-]Inf, [+/-]NaN) in any format specified by C99/XPG6/SUS.
506 \fB\fIn1\fR \fB-le\fR \fIn2\fR\fR
509 True if the number \fIn1\fR is algebraically less than or equal to the number
510 \fIn2\fR. A number may be integer, floating point or floating-point constant
511 (such as [+/-]Inf, [+/-]NaN) in any format specified by C99/XPG6/SUS.
517 \fB\fIcondition1\fR \fB-a\fR \fIcondition2\fR\fR
520 True if both \fIcondition1\fR and \fIcondition2\fR are true. The \fB-a\fR
521 binary primary is left associative and has higher precedence than the \fB-o\fR
528 \fB\fIcondition1\fR \fB-o\fR \fIcondition2\fR\fR
531 True if either \fIcondition1\fR or \fIcondition2\fR is true. The \fB-o\fR
532 binary primary is left associative.
537 These primaries can be combined with the following operators:
541 \fB\fB!\fR \fIcondition\fR\fR
544 True if \fIcondition\fR is false.
550 \fB( \fIcondition\fR )\fR
553 True if condition is true. The parentheses ( ) can be used to alter the normal
554 precedence and associativity. The parentheses are meaningful to the shell and,
555 therefore, must be quoted.
560 The algorithm for determining the precedence of the operators and the return
561 value that is generated is based on the number of arguments presented to
562 \fBtest\fR. (However, when using the \fB[...]\fR form, the right-bracket final
563 argument is not counted in this algorithm.)
566 In the following list, \fB$1\fR, \fB$2\fR, \fB$3\fR and \fB$4\fR represent the
567 arguments presented to \fBtest\fR as a \fIcondition\fR, \fIcondition1\fR, or
572 \fB\fI0 arguments:\fR\fR
581 \fB\fI1 argument:\fR\fR
584 Exit true (0) if \fB$1\fR is not null. Otherwise, exit false.
590 \fB\fI2 arguments:\fR\fR
597 If \fB$1\fR is \fB!\fR, exit true if \fB$2\fR is null, false if \fB$2\fR is not
604 If \fB$1\fR is a unary primary, exit true if the unary test is true, false if
605 the unary test is false.
611 Otherwise, produce unspecified results.
618 \fB\fI3 arguments:\fR\fR
625 If \fB$2\fR is a binary primary, perform the binary test of \fB$1\fR and
632 If \fB$1\fR is \fB!\fR, negate the two-argument test of \fB$2\fR and \fB$3\fR.
638 Otherwise, produce unspecified results.
645 \fB\fI4 arguments:\fR\fR
652 If \fB$1\fR is \fB!\fR, negate the three-argument test of \fB$2\fR, \fB$3\fR,
659 Otherwise, the results are unspecified.
666 Scripts should be careful when dealing with user-supplied input that could be
667 confused with primaries and operators. Unless the application writer knows all
668 the cases that produce input to the script, invocations like \fBtest "$1" -a
669 "$2"\fR should be written as \fBtest "$1" && test "$2"\fR to avoid problems if
670 a user supplied values such as \fB$1\fR set to \fB!\fR and \fB$2\fR set to the
671 null string. That is, in cases where maximal portability is of concern, replace
672 \fBtest expr1 -a expr2\fR with \fBtest expr1 && test expr2\fR, and replace
673 \fBtest expr1 -o expr2\fR with \fBtest expr1 || test expr2\fR. But notice that,
674 in \fBtest\fR, \fB-a\fR has \fBhigher\fR precedence than \fB-o\fR, while
675 \fB&&\fR and \fB||\fR have \fBequal\fR precedence in the shell.
678 Parentheses or braces can be used in the shell command language to effect
682 Parentheses must be escaped when using \fBsh\fR. For example:
686 test \e( expr1 -a expr2 \e) -o expr3
692 This command is not always portable outside XSI-conformant systems. The
693 following form can be used instead:
697 ( test expr1 && test expr2 ) || test expr3
714 could not be used reliably on some historical systems. Unexpected results would
715 occur if such a \fIstring\fR condition were used and \fB$1\fR expanded to
716 \fB!\fR, \fB(\fR, or a known unary primary. Better constructs are,
728 Historical systems have also been unreliable given the common construct:
732 test "$response" = "expected string"
738 One of the following is a more reliable form:
742 test "X$response" = "Xexpected string"
743 test "expected string" = "$response"
749 The second form assumes that \fBexpected string\fR could not be confused with
750 any unary primary. If \fBexpected string\fR starts with \fB\(mi\fR, \fB(\fR,
751 \fB!\fR or even \fB=\fR, the first form should be used instead. Using the
752 preceding rules without the marked extensions, any of the three comparison
753 forms is reliable, given any input. (However, observe that the strings are
754 quoted in all cases.)
757 Because the string comparison binary primaries, \fB=\fR and \fB!=\fR, have a
758 higher precedence than any unary primary in the >4 argument case, unexpected
759 results can occur if arguments are not properly prepared. For example, in
769 If \fB$1\fR evaluates to a possible directory name of \fB=\fR, the first three
770 arguments are considered a string comparison, which causes a syntax error when
771 the second \fB-d\fR is encountered. is encountered. One of the following forms
772 prevents this; the second is preferred:
776 test \e( -d "$1" \e) -o \e( -d "$2" \e)
777 test -d "$1" || test -d "$2"
783 Also in the >4 argument case:
787 test "$1" = "bat" -a "$2" = "ball"
793 Syntax errors occur if \fB$1\fR evaluates to \fB(\fR or \fB!\fR. One of the
794 following forms prevents this; the third is preferred:
798 test "X$1" = "Xbat" -a "X$2" = "Xball"
799 test "$1" = "bat" && test "$2" = "ball"
800 test "X$1" = "Xbat" && test "X$2" = "Xball"
807 In the \fBif\fR command examples, three conditions are tested, and if all three
808 evaluate as true or successful, then their validities are written to the
809 screen. The three tests are:
814 if a variable set to 1 is greater than 0,
820 if a variable set to 2 is equal to 2, and
826 if the word \fBroot\fR is included in the text file \fB/etc/passwd\fR.
830 \fBExample 1 \fRUsing /usr/bin/test
833 Perform a \fBmkdir\fR if a directory does not exist:
838 test ! -d tempdir && mkdir tempdir
844 Wait for a file to become non-readable:
849 while test -r thefile
853 echo'"thefile" is no longer readable'
859 Perform a command if the argument is one of three strings (two variations),
860 using the open bracket version \fB[\fR of the \fBtest\fR command:
865 if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ]
870 pear|grape|apple) command;;
876 \fBExample 2 \fRUsing /usr/bin/test for the -e option
879 If one really wants to use the \fB-e\fR option in \fBsh\fR, use
880 \fB/usr/bin/test\fR, as in the following:
885 if [ ! -h $PKG_INSTALL_ROOT$rLink ] && /usr/bin/test -e
886 $PKG_INSTALL_ROOT/usr/bin/$rFile ; then
887 ln -s $rFile $PKG_INSTALL_ROOT$rLink
892 .SS "The test built-in"
895 The two forms of the \fBtest\fR built-in follow the Bourne shell's \fBif\fR
898 \fBExample 3 \fRUsing the sh built-in
902 ZERO=0 ONE=1 TWO=2 ROOT=root
904 if [ $ONE \fB-gt\fR $ZERO ]
908 grep $ROOT /etc/passwd >&1 > /dev/null \fI# discard output\fR
912 echo "$ONE is greater than 0, $TWO equals 2, and $ROOT is" \e
913 "a user-name in the password file"
917 echo "At least one of the three test conditions is false"
923 \fBExample 4 \fRUsing the test built-in
926 Examples of the \fBtest\fR built-in:
931 test \(gagrep $ROOT /etc/passwd >&1 /dev/null\(ga \fI# discard output\fR
933 echo $? \fI# test for success\fR
934 [ \(gagrep nosuchname /etc/passwd >&1 /dev/null\(ga ]
936 echo $? \fI# test for failure\fR
942 \fBExample 5 \fRUsing the csh built-in
946 @ ZERO = 0; @ ONE = 1; @ TWO = 2; set ROOT = root
947 grep $ROOT /etc/passwd >&1 /dev/null \fI# discard output\fR
948 \fI# $status must be tested for immediately following grep\fR
949 if ( "$status" == "0" && $ONE > $ZERO && $TWO == 2 ) then
950 echo "$ONE is greater than 0, $TWO equals 2, and $ROOT is" \e
951 "a user-name in the password file"
958 \fBExample 6 \fRUsing the ksh/ksh93 built-in
962 ZERO=0 ONE=1 TWO=$((ONE+ONE)) ROOT=root
963 if ((ONE > ZERO)) \fI# arithmetical comparison\fR
964 [[ $TWO = 2 ]] \fI# string comparison\fR
965 [ \(gagrep $ROOT /etc/passwd >&1 /dev/null\(ga ] \fI# discard output\fR
967 echo "$ONE is greater than 0, $TWO equals 2, and $ROOT is" \e
968 "a user-name in the password file"
971 echo "At least one of the three test conditions is false"
976 .SH ENVIRONMENT VARIABLES
979 See \fBenviron\fR(5) for descriptions of the following environment variables
980 that affect the execution of \fBtest\fR: \fBLANG\fR, \fBLC_ALL\fR,
981 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
985 The following exit values are returned:
992 \fIcondition\fR evaluated to true.
1001 \fIcondition\fR evaluated to false or \fIcondition\fR was missing.
1016 See \fBattributes\fR(5) for descriptions of the following attributes:
1017 .SS "/usr/bin/test, csh, ksh, sh"
1025 ATTRIBUTE TYPE ATTRIBUTE VALUE
1027 Interface Stability Committed
1029 Standard See \fBstandards\fR(5).
1040 ATTRIBUTE TYPE ATTRIBUTE VALUE
1042 Interface Stability Uncommitted
1048 \fBcsh\fR(1), \fBksh\fR(1), \fBksh93\fR(1), \fBsh\fR(1), \fBtest\fR(1B),
1049 \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
1053 The \fBnot\(mia\(midirectory\fR alternative to the \fB-f\fR option is a
1054 transition aid for \fBBSD\fR applications and may not be supported in future
1056 .SS "XPG4 \fBsh\fR, \fBksh\fR, \fBksh93\fR"
1059 Use arithmetic expressions such as
1074 $ /usr/bin/test "$x" -gt 3.1 # )
1081 when comparing two floating-point variables or a constant and a floating-point
1082 variable to prevent rounding errors (caused by the base16 to base10
1083 transformation) to affect the result. Additionally the built-in arithmetic
1084 support in XPG4 \fBsh\fR, \fBksh\fR and \fBksh93\fR is significantly faster
1085 because it does not require the explicit transformation to strings for each