1.\"- 2.\" Copyright (c) 1991, 1993 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" This code is derived from software contributed to Berkeley by 6.\" the Institute of Electrical and Electronics Engineers, Inc. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)test.1 8.1 (Berkeley) 5/31/93 33.\" 34.Dd October 5, 2016 35.Dt TEST 1 36.Os 37.Sh NAME 38.Nm test , 39.Nm \&[ 40.Nd condition evaluation utility 41.Sh SYNOPSIS 42.Nm 43.Ar expression 44.Nm \&[ 45.Ar expression Cm \&] 46.Sh DESCRIPTION 47The 48.Nm 49utility evaluates the expression and, if it evaluates 50to true, returns a zero (true) exit status; otherwise 51it returns 1 (false). 52If there is no expression, 53.Nm 54also 55returns 1 (false). 56.Pp 57All operators and flags are separate arguments to the 58.Nm 59utility. 60.Pp 61The following primaries are used to construct expression: 62.Bl -tag -width Ar 63.It Fl b Ar file 64True if 65.Ar file 66exists and is a block special 67file. 68.It Fl c Ar file 69True if 70.Ar file 71exists and is a character 72special file. 73.It Fl d Ar file 74True if 75.Ar file 76exists and is a directory. 77.It Fl e Ar file 78True if 79.Ar file 80exists (regardless of type). 81.It Fl f Ar file 82True if 83.Ar file 84exists and is a regular file. 85.It Fl g Ar file 86True if 87.Ar file 88exists and its set group ID flag 89is set. 90.It Fl h Ar file 91True if 92.Ar file 93exists and is a symbolic link. 94This operator is retained for compatibility with previous versions of 95this program. 96Do not rely on its existence; use 97.Fl L 98instead. 99.It Fl k Ar file 100True if 101.Ar file 102exists and its sticky bit is set. 103.It Fl n Ar string 104True if the length of 105.Ar string 106is nonzero. 107.It Fl p Ar file 108True if 109.Ar file 110is a named pipe 111.Pq Tn FIFO . 112.It Fl r Ar file 113True if 114.Ar file 115exists and is readable. 116.It Fl s Ar file 117True if 118.Ar file 119exists and has a size greater 120than zero. 121.It Fl t Ar file_descriptor 122True if the file whose file descriptor number 123is 124.Ar file_descriptor 125is open and is associated with a terminal. 126.It Fl u Ar file 127True if 128.Ar file 129exists and its set user ID flag 130is set. 131.It Fl w Ar file 132True if 133.Ar file 134exists and is writable. 135True 136indicates only that the write flag is on. 137The file is not writable on a read-only file 138system even if this test indicates true. 139.It Fl x Ar file 140True if 141.Ar file 142exists and is executable. 143True 144indicates only that the execute flag is on. 145If 146.Ar file 147is a directory, true indicates that 148.Ar file 149can be searched. 150.It Fl z Ar string 151True if the length of 152.Ar string 153is zero. 154.It Fl L Ar file 155True if 156.Ar file 157exists and is a symbolic link. 158.It Fl O Ar file 159True if 160.Ar file 161exists and its owner matches the effective user id of this process. 162.It Fl G Ar file 163True if 164.Ar file 165exists and its group matches the effective group id of this process. 166.It Fl S Ar file 167True if 168.Ar file 169exists and is a socket. 170.It Ar file1 Fl nt Ar file2 171True if 172.Ar file1 173exists and is newer than 174.Ar file2 . 175.It Ar file1 Fl ot Ar file2 176True if 177.Ar file1 178exists and is older than 179.Ar file2 . 180.It Ar file1 Fl ef Ar file2 181True if 182.Ar file1 183and 184.Ar file2 185exist and refer to the same file. 186.It Ar string 187True if 188.Ar string 189is not the null 190string. 191.It Ar s1 Cm = Ar s2 192True if the strings 193.Ar s1 194and 195.Ar s2 196are identical. 197.It Ar s1 Cm != Ar s2 198True if the strings 199.Ar s1 200and 201.Ar s2 202are not identical. 203.It Ar s1 Cm < Ar s2 204True if string 205.Ar s1 206comes before 207.Ar s2 208based on the binary value of their characters. 209.It Ar s1 Cm > Ar s2 210True if string 211.Ar s1 212comes after 213.Ar s2 214based on the binary value of their characters. 215.It Ar n1 Fl eq Ar n2 216True if the integers 217.Ar n1 218and 219.Ar n2 220are algebraically 221equal. 222.It Ar n1 Fl ne Ar n2 223True if the integers 224.Ar n1 225and 226.Ar n2 227are not 228algebraically equal. 229.It Ar n1 Fl gt Ar n2 230True if the integer 231.Ar n1 232is algebraically 233greater than the integer 234.Ar n2 . 235.It Ar n1 Fl ge Ar n2 236True if the integer 237.Ar n1 238is algebraically 239greater than or equal to the integer 240.Ar n2 . 241.It Ar n1 Fl lt Ar n2 242True if the integer 243.Ar n1 244is algebraically less 245than the integer 246.Ar n2 . 247.It Ar n1 Fl le Ar n2 248True if the integer 249.Ar n1 250is algebraically less 251than or equal to the integer 252.Ar n2 . 253.El 254.Pp 255If 256.Ar file 257is a symbolic link, 258.Nm 259will fully dereference it and then evaluate the expression 260against the file referenced, except for the 261.Fl h 262and 263.Fl L 264primaries. 265.Pp 266These primaries can be combined with the following operators: 267.Bl -tag -width Ar 268.It Cm \&! Ar expression 269True if 270.Ar expression 271is false. 272.It Ar expression1 Fl a Ar expression2 273True if both 274.Ar expression1 275and 276.Ar expression2 277are true. 278.It Ar expression1 Fl o Ar expression2 279True if either 280.Ar expression1 281or 282.Ar expression2 283are true. 284.It Cm \&( Ar expression Cm \&) 285True if expression is true. 286.El 287.Pp 288The 289.Fl a 290operator has higher precedence than the 291.Fl o 292operator. 293.Pp 294Some shells may provide a builtin 295.Nm 296command which is similar or identical to this utility. 297Consult the 298.Xr builtin 1 299manual page. 300.Sh GRAMMAR AMBIGUITY 301The 302.Nm 303grammar is inherently ambiguous. 304In order to assure a degree of consistency, 305the cases described in the 306.St -p1003.2 , 307section D11.2/4.62.4, standard 308are evaluated consistently according to the rules specified in the 309standards document. 310All other cases are subject to the ambiguity in the 311command semantics. 312.Pp 313In particular, only expressions containing 314.Fl a , 315.Fl o , 316.Cm \&( 317or 318.Cm \&) 319can be ambiguous. 320.Sh EXIT STATUS 321The 322.Nm 323utility exits with one of the following values: 324.Bl -tag -width indent 325.It 0 326expression evaluated to true. 327.It 1 328expression evaluated to false or expression was 329missing. 330.It >1 331An error occurred. 332.El 333.Sh EXAMPLES 334Implement 335.Li test FILE1 -nt FILE2 336using only 337.Tn POSIX 338functionality: 339.Pp 340.Dl test -n \&"$(find -L -- FILE1 -prune -newer FILE2 2>/dev/null)\&" 341.Pp 342This can be modified using non-standard 343.Xr find 1 344primaries like 345.Cm -newerca 346to compare other timestamps. 347.Sh COMPATIBILITY 348For compatibility with some other implementations, 349the 350.Cm = 351primary can be substituted with 352.Cm == 353with the same meaning. 354.Sh SEE ALSO 355.Xr builtin 1 , 356.Xr expr 1 , 357.Xr find 1 , 358.Xr sh 1 , 359.Xr stat 1 , 360.Xr symlink 7 361.Sh STANDARDS 362The 363.Nm 364utility implements a superset of the 365.St -p1003.2 366specification. 367The primaries 368.Cm < , 369.Cm == , 370.Cm > , 371.Fl ef , 372.Fl nt , 373.Fl ot , 374.Fl G , 375and 376.Fl O 377are extensions. 378.Sh HISTORY 379A 380.Nm 381utility appeared in 382.At v7 . 383.Sh BUGS 384Both sides are always evaluated in 385.Fl a 386and 387.Fl o . 388For instance, the writable status of 389.Pa file 390will be tested by the following command even though the former expression 391indicated false, which results in a gratuitous access to the file system: 392.Dl "[ -z abc -a -w file ]" 393To avoid this, write 394.Dl "[ -z abc ] && [ -w file ]" 395