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