]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/test.def
Bash-4.2 distribution sources and documentation
[thirdparty/bash.git] / builtins / test.def
CommitLineData
726f6388
JA
1This file is test.def, from which is created test.c.
2It implements the builtin "test" in Bash.
3
495aee44 4Copyright (C) 1987-2010 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
726f6388 12
3185942a
JA
13Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$PRODUCES test.c
22
23$BUILTIN test
24$FUNCTION test_builtin
25$SHORT_DOC test [expr]
3185942a
JA
26Evaluate conditional expression.
27
7117c2d2 28Exits with a status of 0 (true) or 1 (false) depending on
726f6388
JA
29the evaluation of EXPR. Expressions may be unary or binary. Unary
30expressions are often used to examine the status of a file. There
495aee44
CR
31are string operators and numeric comparison operators as well.
32
33The behavior of test depends on the number of arguments. Read the
34bash manual page for the complete specification.
726f6388
JA
35
36File operators:
37
3185942a
JA
38 -a FILE True if file exists.
39 -b FILE True if file is block special.
40 -c FILE True if file is character special.
41 -d FILE True if file is a directory.
42 -e FILE True if file exists.
43 -f FILE True if file exists and is a regular file.
44 -g FILE True if file is set-group-id.
45 -h FILE True if file is a symbolic link.
46 -L FILE True if file is a symbolic link.
47 -k FILE True if file has its `sticky' bit set.
48 -p FILE True if file is a named pipe.
49 -r FILE True if file is readable by you.
50 -s FILE True if file exists and is not empty.
51 -S FILE True if file is a socket.
52 -t FD True if FD is opened on a terminal.
53 -u FILE True if the file is set-user-id.
54 -w FILE True if the file is writable by you.
55 -x FILE True if the file is executable by you.
56 -O FILE True if the file is effectively owned by you.
57 -G FILE True if the file is effectively owned by your group.
58 -N FILE True if the file has been modified since it was last read.
726f6388 59
cce855bc
JA
60 FILE1 -nt FILE2 True if file1 is newer than file2 (according to
61 modification date).
726f6388
JA
62
63 FILE1 -ot FILE2 True if file1 is older than file2.
64
65 FILE1 -ef FILE2 True if file1 is a hard link to file2.
66
67String operators:
68
3185942a 69 -z STRING True if string is empty.
726f6388 70
3185942a
JA
71 -n STRING
72 STRING True if string is not empty.
726f6388 73
3185942a
JA
74 STRING1 = STRING2
75 True if the strings are equal.
76 STRING1 != STRING2
77 True if the strings are not equal.
78 STRING1 < STRING2
79 True if STRING1 sorts before STRING2 lexicographically.
80 STRING1 > STRING2
81 True if STRING1 sorts after STRING2 lexicographically.
726f6388
JA
82
83Other operators:
84
3185942a 85 -o OPTION True if the shell option OPTION is enabled.
495aee44 86 -v VAR True if the shell variable VAR is set
3185942a
JA
87 ! EXPR True if expr is false.
88 EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
89 EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
726f6388 90
3185942a
JA
91 arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
92 -lt, -le, -gt, or -ge.
726f6388
JA
93
94Arithmetic binary operators return true if ARG1 is equal, not-equal,
95less-than, less-than-or-equal, greater-than, or greater-than-or-equal
96than ARG2.
3185942a
JA
97
98Exit Status:
99Returns success if EXPR evaluates to true; fails if EXPR evaluates to
100false or an invalid argument is given.
726f6388
JA
101$END
102
103$BUILTIN [
104$DOCNAME test_bracket
105$FUNCTION test_builtin
106$SHORT_DOC [ arg... ]
3185942a
JA
107Evaluate conditional expression.
108
109This is a synonym for the "test" builtin, but the last argument must
110be a literal `]', to match the opening `['.
726f6388
JA
111$END
112
ccc6cda3
JA
113#include <config.h>
114
115#if defined (HAVE_UNISTD_H)
cce855bc
JA
116# ifdef _MINIX
117# include <sys/types.h>
118# endif
ccc6cda3
JA
119# include <unistd.h>
120#endif
121
122#include "../bashansi.h"
3185942a 123#include "../bashintl.h"
726f6388
JA
124
125#include "../shell.h"
cce855bc 126#include "../test.h"
ccc6cda3
JA
127#include "common.h"
128
726f6388
JA
129extern char *this_command_name;
130
131/* TEST/[ builtin. */
132int
133test_builtin (list)
134 WORD_LIST *list;
135{
136 char **argv;
137 int argc, result;
726f6388
JA
138
139 /* We let Matthew Bradburn and Kevin Braunsdorf's code do the
140 actual test command. So turn the list of args into an array
ccc6cda3
JA
141 of strings, since that is what their code wants. */
142 if (list == 0)
726f6388
JA
143 {
144 if (this_command_name[0] == '[' && !this_command_name[1])
b72432fd 145 {
3185942a 146 builtin_error (_("missing `]'"));
b72432fd
JA
147 return (EX_BADUSAGE);
148 }
726f6388
JA
149
150 return (EXECUTION_FAILURE);
151 }
152
ccc6cda3 153 argv = make_builtin_argv (list, &argc);
726f6388 154 result = test_command (argc, argv);
ccc6cda3
JA
155 free ((char *)argv);
156
726f6388
JA
157 return (result);
158}