1TEST(1P)                   POSIX Programmer's Manual                  TEST(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       test — evaluate expression
13

SYNOPSIS

15       test [expression]
16
17       [ [expression] ]
18

DESCRIPTION

20       The test utility shall evaluate the expression and indicate the  result
21       of  the evaluation by its exit status. An exit status of zero indicates
22       that the expression evaluated as true and an exit status of 1 indicates
23       that the expression evaluated as false.
24
25       In  the  second  form  of the utility, where the utility name used is [
26       rather than test, the application shall ensure that the closing  square
27       bracket  is a separate argument. The test and [ utilities may be imple‐
28       mented as a single linked utility which examines the  basename  of  the
29       zeroth command line argument to determine whether to behave as the test
30       or [ variant. Applications using the exec() family of functions to exe‐
31       cute  these  utilities shall ensure that the argument passed in arg0 or
32       argv[0] is '[' when executing the [  utility  and  has  a  basename  of
33       "test" when executing the test utility.
34

OPTIONS

36       The  test  utility  shall not recognize the "--" argument in the manner
37       specified  by  Guideline  10  in  the  Base   Definitions   volume   of
38       POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines.
39
40       No options shall be supported.
41

OPERANDS

43       The  application  shall  ensure that all operators and elements of pri‐
44       maries are presented as separate arguments to the test utility.
45
46       The following primaries can be used to construct expression:
47
48       -b pathname
49                 True if pathname resolves to an existing directory entry  for
50                 a  block special file.  False if pathname cannot be resolved,
51                 or if pathname resolves to an existing directory entry for  a
52                 file that is not a block special file.
53
54       -c pathname
55                 True  if pathname resolves to an existing directory entry for
56                 a character  special  file.   False  if  pathname  cannot  be
57                 resolved,  or  if  pathname resolves to an existing directory
58                 entry for a file that is not a character special file.
59
60       -d pathname
61                 True if pathname resolves to an existing directory entry  for
62                 a  directory.  False  if  pathname  cannot be resolved, or if
63                 pathname resolves to an existing directory entry for  a  file
64                 that is not a directory.
65
66       -e pathname
67                 True  if  pathname  resolves  to an existing directory entry.
68                 False if pathname cannot be resolved.
69
70       -f pathname
71                 True if pathname resolves to an existing directory entry  for
72                 a  regular  file. False if pathname cannot be resolved, or if
73                 pathname resolves to an existing directory entry for  a  file
74                 that is not a regular file.
75
76       -g pathname
77                 True  if pathname resolves to an existing directory entry for
78                 a file that has its set-group-ID flag set. False if  pathname
79                 cannot  be  resolved,  or if pathname resolves to an existing
80                 directory entry for a file that does not have its  set-group-
81                 ID flag set.
82
83       -h pathname
84                 True  if pathname resolves to an existing directory entry for
85                 a symbolic link. False if pathname cannot be resolved, or  if
86                 pathname  resolves  to an existing directory entry for a file
87                 that is not a symbolic link. If the final component of  path‐
88                 name is a symbolic link, that symbolic link is not followed.
89
90       -L pathname
91                 True  if pathname resolves to an existing directory entry for
92                 a symbolic link. False if pathname cannot be resolved, or  if
93                 pathname  resolves  to an existing directory entry for a file
94                 that is not a symbolic link. If the final component of  path‐
95                 name is a symbolic link, that symbolic link is not followed.
96
97       -n string True if the length of string is non-zero; otherwise, false.
98
99       -p pathname
100                 True  if pathname resolves to an existing directory entry for
101                 a FIFO. False if pathname cannot be resolved, or if  pathname
102                 resolves  to  an  existing directory entry for a file that is
103                 not a FIFO.
104
105       -r pathname
106                 True if pathname resolves to an existing directory entry  for
107                 a  file  for  which  permission to read from the file will be
108                 granted, as defined in Section 1.1.1.4, File Read, Write, and
109                 Creation.   False if pathname cannot be resolved, or if path‐
110                 name resolves to an existing directory entry for a  file  for
111                 which permission to read from the file will not be granted.
112
113       -S pathname
114                 True  if pathname resolves to an existing directory entry for
115                 a socket. False if pathname cannot be resolved, or  if  path‐
116                 name  resolves to an existing directory entry for a file that
117                 is not a socket.
118
119       -s pathname
120                 True if pathname resolves to an existing directory entry  for
121                 a  file  that has a size greater than zero. False if pathname
122                 cannot be resolved, or if pathname resolves  to  an  existing
123                 directory  entry for a file that does not have a size greater
124                 than zero.
125
126       -t file_descriptor
127                 True if file descriptor number file_descriptor is open and is
128                 associated with a terminal. False if file_descriptor is not a
129                 valid file descriptor number, or if  file  descriptor  number
130                 file_descriptor  is  not  open,  or  if it is open but is not
131                 associated with a terminal.
132
133       -u pathname
134                 True if pathname resolves to an existing directory entry  for
135                 a  file  that has its set-user-ID flag set. False if pathname
136                 cannot be resolved, or if pathname resolves  to  an  existing
137                 directory entry for a file that does not have its set-user-ID
138                 flag set.
139
140       -w pathname
141                 True if pathname resolves to an existing directory entry  for
142                 a  file  for  which  permission  to write to the file will be
143                 granted, as defined in Section 1.1.1.4, File Read, Write, and
144                 Creation.   False if pathname cannot be resolved, or if path‐
145                 name resolves to an existing directory entry for a  file  for
146                 which permission to write to the file will not be granted.
147
148       -x pathname
149                 True  if pathname resolves to an existing directory entry for
150                 a file for which permission to execute the  file  (or  search
151                 it, if it is a directory) will be granted, as defined in Sec‐
152                 tion 1.1.1.4, File Read, Write, and Creation.  False if path‐
153                 name cannot be resolved, or if pathname resolves to an exist‐
154                 ing directory entry for a file for which permission  to  exe‐
155                 cute (or search) the file will not be granted.
156
157       -z string True  if  the  length  of  string  string is zero; otherwise,
158                 false.
159
160       string    True if the string string is not the null string;  otherwise,
161                 false.
162
163       s1 = s2   True  if  the  strings  s1  and  s2 are identical; otherwise,
164                 false.
165
166       s1 != s2  True if the strings s1 and s2 are not  identical;  otherwise,
167                 false.
168
169       n1 -eq n2 True  if the integers n1 and n2 are algebraically equal; oth‐
170                 erwise, false.
171
172       n1 -ne n2 True if the integers n1 and n2 are not  algebraically  equal;
173                 otherwise, false.
174
175       n1 -gt n2 True  if  the  integer  n1  is algebraically greater than the
176                 integer n2; otherwise, false.
177
178       n1 -ge n2 True if the integer n1 is algebraically greater than or equal
179                 to the integer n2; otherwise, false.
180
181       n1 -lt n2 True if the integer n1 is algebraically less than the integer
182                 n2; otherwise, false.
183
184       n1 -le n2 True if the integer n1 is algebraically less than or equal to
185                 the integer n2; otherwise, false.
186
187       expression1 -a expression2
188                 True if both expression1 and expression2 are true; otherwise,
189                 false. The -a binary primary is left associative.  It  has  a
190                 higher precedence than -o.
191
192       expression1 -o expression2
193                 True if either expression1 or expression2 is true; otherwise,
194                 false. The -o binary primary is left associative.
195
196       With the exception of the -h pathname and -L pathname primaries,  if  a
197       pathname  argument  is a symbolic link, test shall evaluate the expres‐
198       sion by resolving the symbolic link and using the  file  referenced  by
199       the link.
200
201       These primaries can be combined with the following operators:
202
203       ! expression
204                 True if expression is false. False if expression is true.
205
206       ( expression )
207                 True if expression is true. False if expression is false. The
208                 parentheses can be used to alter the  normal  precedence  and
209                 associativity.
210
211       The primaries with two elements of the form:
212
213
214           -primary_operator primary_operand
215
216       are  known  as  unary  primaries.  The primaries with three elements in
217       either of the two forms:
218
219
220           primary_operand -primary_operator primary_operand
221
222           primary_operand primary_operator primary_operand
223
224       are known as binary primaries.  Additional implementation-defined oper‐
225       ators  and  primary_operators  may be provided by implementations. They
226       shall be of the form -operator where the first character of operator is
227       not a digit.
228
229       The  algorithm  for determining the precedence of the operators and the
230       return value that shall be generated is based on the  number  of  argu‐
231       ments  presented  to  test.  (However, when using the "[...]" form, the
232       <right-square-bracket> final argument shall  not  be  counted  in  this
233       algorithm.)
234
235       In  the following list, $1, $2, $3, and $4 represent the arguments pre‐
236       sented to test:
237
238       0 arguments:
239                   Exit false (1).
240
241       1 argument: Exit true (0) if $1 is not null; otherwise, exit false.
242
243       2 arguments:
244                    *  If $1 is '!', exit true if $2 is null, false if  $2  is
245                       not null.
246
247                    *  If  $1  is a unary primary, exit true if the unary test
248                       is true, false if the unary test is false.
249
250                    *  Otherwise, produce unspecified results.
251
252       3 arguments:
253                    *  If $2 is a binary primary, perform the binary  test  of
254                       $1 and $3.
255
256                    *  If  $1  is  '!', negate the two-argument test of $2 and
257                       $3.
258
259                    *  If $1 is '(' and $3 is ')', perform the unary  test  of
260                       $2.  On systems that do not support the XSI option, the
261                       results are unspecified if $1 is '(' and $3 is ')'.
262
263                    *  Otherwise, produce unspecified results.
264
265       4 arguments:
266                    *  If $1 is '!', negate the three-argument test of $2, $3,
267                       and $4.
268
269                    *  If  $1  is  '(' and $4 is ')', perform the two-argument
270                       test of $2 and $3.  On systems that do not support  the
271                       XSI  option,  the  results are unspecified if $1 is '('
272                       and $4 is ')'.
273
274                    *  Otherwise, the results are unspecified.
275
276       >4 arguments:
277                   The results are unspecified.
278
279                   On XSI-conformant systems, combinations  of  primaries  and
280                   operators shall be evaluated using the precedence and asso‐
281                   ciativity rules  described  previously.  In  addition,  the
282                   string  comparison binary primaries '=' and "!=" shall have
283                   a higher precedence than any unary primary.
284

STDIN

286       Not used.
287

INPUT FILES

289       None.
290

ENVIRONMENT VARIABLES

292       The following environment variables shall affect the execution of test:
293
294       LANG      Provide a default value for  the  internationalization  vari‐
295                 ables  that are unset or null. (See the Base Definitions vol‐
296                 ume of POSIX.1‐2017, Section 8.2, Internationalization  Vari‐
297                 ables  for  the  precedence of internationalization variables
298                 used to determine the values of locale categories.)
299
300       LC_ALL    If set to a non-empty string value, override  the  values  of
301                 all the other internationalization variables.
302
303       LC_CTYPE  Determine  the  locale for the interpretation of sequences of
304                 bytes of text data as characters (for example, single-byte as
305                 opposed to multi-byte characters in arguments).
306
307       LC_MESSAGES
308                 Determine the locale that should be used to affect the format
309                 and contents  of  diagnostic  messages  written  to  standard
310                 error.
311
312       NLSPATH   Determine the location of message catalogs for the processing
313                 of LC_MESSAGES.
314

ASYNCHRONOUS EVENTS

316       Default.
317

STDOUT

319       Not used.
320

STDERR

322       The standard error shall be used only for diagnostic messages.
323

OUTPUT FILES

325       None.
326

EXTENDED DESCRIPTION

328       None.
329

EXIT STATUS

331       The following exit values shall be returned:
332
333        0    expression evaluated to true.
334
335        1    expression evaluated to false or expression was missing.
336
337       >1    An error occurred.
338

CONSEQUENCES OF ERRORS

340       Default.
341
342       The following sections are informative.
343

APPLICATION USAGE

345       The XSI extensions specifying the -a and -o binary  primaries  and  the
346       '('  and  ')' operators have been marked obsolescent. (Many expressions
347       using them are ambiguously defined by the grammar depending on the spe‐
348       cific  expressions  being  evaluated.)  Scripts using these expressions
349       should be converted to the forms given below. Even though  many  imple‐
350       mentations  will  continue  to support these obsolescent forms, scripts
351       should be extremely careful when dealing with user-supplied input  that
352       could  be confused with these and other primaries and operators. Unless
353       the application developer knows all the cases that produce input to the
354       script, invocations like:
355
356
357           test "$1" -a "$2"
358
359       should be written as:
360
361
362           test "$1" && test "$2"
363
364       to  avoid problems if a user supplied values such as $1 set to '!'  and
365       $2 set to the null string. That is, in cases where maximal  portability
366       is of concern, replace:
367
368
369           test expr1 -a expr2
370
371       with:
372
373
374           test expr1 && test expr2
375
376       and replace:
377
378
379           test expr1 -o expr2
380
381       with:
382
383
384           test expr1 || test expr2
385
386       but note that, in test, -a has higher precedence than -o while "&&" and
387       "||" have equal precedence in the shell.
388
389       Parentheses or braces can be used in  the  shell  command  language  to
390       effect grouping.
391
392       Parentheses must be escaped when using sh; for example:
393
394
395           test \( expr1 -a expr2 \) -o expr3
396
397       This  command  is  not  always  portable even on XSI-conformant systems
398       depending on the expressions specified by expr1, expr2, and expr3.  The
399       following form can be used instead:
400
401
402           ( test expr1 && test expr2 ) || test expr3
403
404       The two commands:
405
406
407           test "$1"
408           test ! "$1"
409
410       could  not  be  used  reliably  on  some historical systems. Unexpected
411       results would occur if such  a  string  expression  were  used  and  $1
412       expanded to '!', '(', or a known unary primary. Better constructs are:
413
414
415           test -n "$1"
416           test -z "$1"
417
418       respectively.
419
420       Historical  systems  have  also  been  unreliable given the common con‐
421       struct:
422
423
424           test "$response" = "expected string"
425
426       One of the following is a more reliable form:
427
428
429           test "X$response" = "Xexpected string"
430           test "expected string" = "$response"
431
432       Note that the second form assumes that expected  string  could  not  be
433       confused  with  any  unary primary. If expected string starts with '-',
434       '(', '!', or even '=', the first form should be used instead. Using the
435       preceding  rules  without  the  XSI marked extensions, any of the three
436       comparison forms is reliable, given any input. (However, note that  the
437       strings are quoted in all cases.)
438
439       Because  the  string  comparison binary primaries, '=' and "!=", have a
440       higher precedence than any unary primary in the greater than 4 argument
441       case,  unexpected  results can occur if arguments are not properly pre‐
442       pared. For example, in:
443
444
445           test -d $1 -o -d $2
446
447       If $1 evaluates to a possible directory name of '=',  the  first  three
448       arguments  are considered a string comparison, which shall cause a syn‐
449       tax error when the second -d is encountered. One of the following forms
450       prevents this; the second is preferred:
451
452
453           test \( -d "$1" \) -o \( -d "$2" \)
454           test -d "$1" || test -d "$2"
455
456       Also in the greater than 4 argument case:
457
458
459           test "$1" = "bat" -a "$2" = "ball"
460
461       syntax  errors occur if $1 evaluates to '(' or '!'.  One of the follow‐
462       ing forms prevents this; the third is preferred:
463
464
465           test "X$1" = "Xbat" -a "X$2" = "Xball"
466           test "$1" = "bat" && test "$2" = "ball"
467           test "X$1" = "Xbat" && test "X$2" = "Xball"
468
469       Note that none of the following examples are permitted  by  the  syntax
470       described:
471
472
473           [-f file]
474           [-f file ]
475           [ -f file]
476           [ -f file
477           test -f file ]
478
479       In  the  first  two  cases, if a utility named [‐f exists, that utility
480       would be invoked, and not test.  In the remaining cases,  the  brackets
481       are mismatched, and the behavior is unspecified. However:
482
483
484           test ! ]
485
486       does have a defined meaning, and must exit with status 1. Similarly:
487
488
489           test ]
490
491       must exit with status 0.
492

EXAMPLES

494        1. Exit if there are not two or three arguments (two variations):
495
496
497               if [ $# -ne 2 ] && [ $# -ne 3 ]; then exit 1; fi
498               if [ $# -lt 2 ] || [ $# -gt 3 ]; then exit 1; fi
499
500        2. Perform a mkdir if a directory does not exist:
501
502
503               test ! -d tempdir && mkdir tempdir
504
505        3. Wait for a file to become non-readable:
506
507
508               while test -r thefile
509               do
510                   sleep 30
511               done
512               echo '"thefile" is no longer readable'
513
514        4. Perform  a  command  if  the  argument is one of three strings (two
515           variations):
516
517
518               if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ]
519               then
520                   command
521               fi
522
523               case "$1" in
524                   pear|grape|apple) command ;;
525               esac
526

RATIONALE

528       The KornShell-derived conditional command  (double  bracket  [[]])  was
529       removed  from  the  shell command language description in an early pro‐
530       posal. Objections were raised that the real problem is  misuse  of  the
531       test command ([), and putting it into the shell is the wrong way to fix
532       the problem.  Instead, proper documentation and a  new  shell  reserved
533       word (!)  are sufficient.
534
535       Tests  that  require  multiple test operations can be done at the shell
536       level using individual invocations of the test command and shell  logi‐
537       cals, rather than using the error-prone -o flag of test.
538
539       XSI-conformant systems support more than four arguments.
540
541       XSI-conformant systems support the combining of primaries with the fol‐
542       lowing constructs:
543
544       expression1 -a expression2
545             True if both expression1 and expression2 are true.
546
547       expression1 -o expression2
548             True if at least one of expression1 and expression2 are true.
549
550       ( expression )
551             True if expression is true.
552
553       In evaluating these more complex combined  expressions,  the  following
554       precedence rules are used:
555
556        *  The  unary  primaries  have  higher  precedence  than the algebraic
557           binary primaries.
558
559        *  The unary primaries have lower precedence than  the  string  binary
560           primaries.
561
562        *  The  unary  and  binary  primaries  have higher precedence than the
563           unary string primary.
564
565        *  The !  operator has higher precedence than the -a operator, and the
566           -a operator has higher precedence than the -o operator.
567
568        *  The -a and -o operators are left associative.
569
570        *  The  parentheses  can  be  used  to alter the normal precedence and
571           associativity.
572
573       The BSD and System V versions of -f are not the same. The  BSD  defini‐
574       tion was:
575
576       -f file   True if file exists and is not a directory.
577
578       The  SVID  version  (true if the file exists and is a regular file) was
579       chosen for this volume of POSIX.1‐2017 because its  use  is  consistent
580       with  the  -b,  -c,  -d, and -p operands (file exists and is a specific
581       file type).
582
583       The -e primary, possessing similar functionality to  that  provided  by
584       the  C  shell,  was  added because it provides the only way for a shell
585       script to find out if a file exists without trying to  open  the  file.
586       Since  implementations are allowed to add additional file types, a por‐
587       table script cannot use:
588
589
590           test -b foo -o -c foo -o -d foo -o -f foo -o -p foo
591
592       to find out if foo is an existing file. On historical BSD systems,  the
593       existence of a file could be determined by:
594
595
596           test -f foo -o -d foo
597
598       but there was no easy way to determine that an existing file was a reg‐
599       ular file. An early proposal used the KornShell -a  primary  (with  the
600       same  meaning),  but this was changed to -e because there were concerns
601       about the high probability of humans confusing the -a primary with  the
602       -a binary operator.
603
604       The following options were not included in this volume of POSIX.1‐2017,
605       although they are provided  by  some  implementations.  These  operands
606       should not be used by new implementations for other purposes:
607
608       -k file   True if file exists and its sticky bit is set.
609
610       -C file   True if file is a contiguous file.
611
612       -V file   True if file is a version file.
613
614       The  following  option  was not included because it was undocumented in
615       most  implementations,  has  been  removed  from  some  implementations
616       (including  System  V),  and the functionality is provided by the shell
617       (see Section 2.6.2, Parameter Expansion.
618
619       -l string The length of the string string.
620
621       The -b, -c, -g, -p, -u, and -x operands are derived from the SVID; his‐
622       torical  BSD does not provide them. The -k operand is derived from Sys‐
623       tem V; historical BSD does not provide it.
624
625       On historical BSD systems, test  -w  directory  always  returned  false
626       because  test  tried  to  open  the directory for writing, which always
627       fails.
628
629       Some additional primaries newly invented or from the KornShell appeared
630       in  an  early  proposal as part of the conditional command ([[]]): s1 >
631       s2, s1 < s2, str = pattern, str != pattern, f1 -nt f2, f1 -ot  f2,  and
632       f1  -ef  f2.   They were not carried forward into the test utility when
633       the conditional command was removed from the shell  because  they  have
634       not been included in the test utility built into historical implementa‐
635       tions of the sh utility.
636
637       The -t file_descriptor primary  is  shown  with  a  mandatory  argument
638       because  the  grammar  is  ambiguous  if  it can be omitted. Historical
639       implementations have allowed it to be omitted, providing a  default  of
640       1.
641
642       It  is  noted  that  '[' is not part of the portable filename character
643       set; however, since it is required to be encoded by a single byte,  and
644       is part of the portable character set, the name of this utility forms a
645       character string across all supported locales.
646

FUTURE DIRECTIONS

648       None.
649

SEE ALSO

651       Section 1.1.1.4, File Read, Write, and Creation, find
652
653       The Base Definitions volume of  POSIX.1‐2017,  Chapter  8,  Environment
654       Variables, Section 12.2, Utility Syntax Guidelines
655
657       Portions  of  this text are reprinted and reproduced in electronic form
658       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
659       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
660       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
661       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
662       event of any discrepancy between this version and the original IEEE and
663       The  Open Group Standard, the original IEEE and The Open Group Standard
664       is the referee document. The original Standard can be  obtained  online
665       at http://www.opengroup.org/unix/online.html .
666
667       Any  typographical  or  formatting  errors that appear in this page are
668       most likely to have been introduced during the conversion of the source
669       files  to  man page format. To report such errors, see https://www.ker
670       nel.org/doc/man-pages/reporting_bugs.html .
671
672
673
674IEEE/The Open Group                  2017                             TEST(1P)
Impressum