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