1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
.TH TEST 1
.CT 1 shell
.SH NAME
test, [, newer \- condition commands
.SH SYNOPSIS
.B test
.I expr
.PP
\fB[\fR
.I expr
\fB]\fR
.PP
.B newer
.I file1 file2
.SH DESCRIPTION
.I Test
evaluates the expression
.IR expr .
If the value is true the exit status is 0; otherwise the
exit status is nonzero.
If there are no arguments the exit status is nonzero.
.PP
The following primitives are used to construct
.IR expr .
.TP \w'\fL-t\fI\ fildes\ 'u
.BI -r " file"
True if the file exists (is accessible) and is readable.
.PD0
.TP
.BI -w " file"
True if the file exists and is writable.
.TP
.BI -x " file"
True if the file exists and has execute permission.
.TP
.BI -e " file
True if the file exists.
.TP
.BI -f " file"
True if the file exists and is a plain file.
.TP
.BI -d " file"
True if the file exists and is a directory.
.TP
.BI -c " file"
True if the file exists and is a character special file.
.TP
.BI -b " file"
True if the file exists and is a block special file.
.TP
.BI -L " file"
True if the file is a symbolic link.
.TP
.BI -u " file"
True if the file exists and has set userid permission.
.TP
.BI -g " file"
True if the file exists and has set groupid permission.
.TP
.BI -s " file"
True if the file exists and has a size greater than zero.
.TP
.BI -t " fildes
True if the open file whose file descriptor number is
.I fildes
(1 by default)
is associated with a terminal device.
.TP
.B -S
True if the effective userid is zero.
.TP
.IB s1 " = " s2
True
if the strings
.I s1
and
.I s2
are identical.
.TP
.IB s1 " != " s2
True
if the strings
.I s1
and
.I s2
are not identical.
.TP
s1
True if
.I s1
is not the null string.
(Deprecated.)
.TP
.BI -z " s1"
True if the length of string
.I s1
is zero.
.TP
.IB n1 " -eq " n2
True if the integers
.I n1
and
.I n2
are arithmetically equal.
Any of the comparisons
.BR -ne ,
.BR -gt ,
.BR -ge ,
.BR -lt ,
or
.BR -le
may be used in place of
.BR -eq .
The (nonstandard) construct
.BI -l " string,
meaning the length of
.I string,
may be used in place of an integer.
.PD
.PP
These primaries may be combined with the
following operators:
.TP
.B !
unary negation operator
.PD0
.TP
.B -o
binary
.I or
operator
.TP
.B -a
binary
.I and
operator; higher precedence than
.BR -o
.TP
.BI "( " expr " )"
parentheses for grouping.
.PD
.PP
Notice that all the operators and flags are separate
arguments to
.IR test .
Notice also that parentheses are meaningful
to the Shell and must be escaped.
.PP
.B [
is a synonym for
.I test,
except that
.B [
requires a closing
.BR ] .
.PP
.I Newer
returns a zero exit code if
.I file1
exists and
.I file2
does not, or if
.I file1
and
.I file2
both exist and
.I file1
was modified at least as recently
as
.IR file2 .
It returns a non-zero return code otherwise.
.SH EXAMPLES
.I Test
is a dubious way to check for specific character strings:
it uses a process to do what a shell case statement can do.
The first example is not only inefficient but wrong, because
.I test
understands the purported string
.B \&"-c"
as an option.
Furthermore
.B $1
might be empty.
.IP
.EX
if test $1 = "-c" # wrong!
then echo OK
fi
.EE
.LP
A correct way is
.IP
.EX
case "$1" in
-c) echo OK
esac
.EE
.PP
Test whether
.L abc
is in the current directory.
.IP
.B test -e abc -o -L abc
.SH "SEE ALSO"
.IR sh (1),
.IR find (1)
|