1COMMAND(P) POSIX Programmer's Manual COMMAND(P)
2
3
4
6 command - execute a simple command
7
9 command [-p] command_name [argument ...]
10
11
12
13 command [ -v | -V ] command_name
14
15
17 The command utility shall cause the shell to treat the arguments as a
18 simple command, suppressing the shell function lookup that is described
19 in Command Search and Execution , item 1b.
20
21 If the command_name is the same as the name of one of the special
22 built-in utilities, the special properties in the enumerated list at
23 the beginning of Special Built-In Utilities shall not occur. In every
24 other respect, if command_name is not the name of a function, the
25 effect of command (with no options) shall be the same as omitting com‐
26 mand.
27
28 On systems supporting the User Portability Utilities option, the com‐
29 mand utility also shall provide information concerning how a command
30 name is interpreted by the shell; see -v and -V.
31
33 The command utility shall conform to the Base Definitions volume of
34 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
35
36 The following options shall be supported:
37
38 -p Perform the command search using a default value for PATH that
39 is guaranteed to find all of the standard utilities.
40
41 -v (On systems supporting the User Portability Utilities option.)
42 Write a string to standard output that indicates the pathname or
43 command that will be used by the shell, in the current shell
44 execution environment (see Shell Execution Environment ), to
45 invoke command_name, but do not invoke command_name.
46
47 * Utilities, regular built-in utilities, command_names includ‐
48 ing a slash character, and any implementation-defined func‐
49 tions that are found using the PATH variable (as described in
50 Command Search and Execution ), shall be written as absolute
51 pathnames.
52
53 * Shell functions, special built-in utilities, regular built-in
54 utilities not associated with a PATH search, and shell
55 reserved words shall be written as just their names.
56
57 * An alias shall be written as a command line that represents
58 its alias definition.
59
60 * Otherwise, no output shall be written and the exit status
61 shall reflect that the name was not found.
62
63 -V (On systems supporting the User Portability Utilities option.)
64 Write a string to standard output that indicates how the name
65 given in the command_name operand will be interpreted by the
66 shell, in the current shell execution environment (see Shell
67 Execution Environment ), but do not invoke command_name.
68 Although the format of this string is unspecified, it shall
69 indicate in which of the following categories command_name falls
70 and shall include the information stated:
71
72 * Utilities, regular built-in utilities, and any implementa‐
73 tion-defined functions that are found using the PATH variable
74 (as described in Command Search and Execution ), shall be
75 identified as such and include the absolute pathname in the
76 string.
77
78 * Other shell functions shall be identified as functions.
79
80 * Aliases shall be identified as aliases and their definitions
81 included in the string.
82
83 * Special built-in utilities shall be identified as special
84 built-in utilities.
85
86 * Regular built-in utilities not associated with a PATH search
87 shall be identified as regular built-in utilities. (The term
88 "regular" need not be used.)
89
90 * Shell reserved words shall be identified as reserved words.
91
93 The following operands shall be supported:
94
95 argument
96 One of the strings treated as an argument to command_name.
97
98 command_name
99
100 The name of a utility or a special built-in utility.
101
102
104 Not used.
105
107 None.
108
110 The following environment variables shall affect the execution of com‐
111 mand:
112
113 LANG Provide a default value for the internationalization variables
114 that are unset or null. (See the Base Definitions volume of
115 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
116 ables for the precedence of internationalization variables used
117 to determine the values of locale categories.)
118
119 LC_ALL If set to a non-empty string value, override the values of all
120 the other internationalization variables.
121
122 LC_CTYPE
123 Determine the locale for the interpretation of sequences of
124 bytes of text data as characters (for example, single-byte as
125 opposed to multi-byte characters in arguments).
126
127 LC_MESSAGES
128 Determine the locale that should be used to affect the format
129 and contents of diagnostic messages written to standard error
130 and informative messages written to standard output.
131
132 NLSPATH
133 Determine the location of message catalogs for the processing of
134 LC_MESSAGES .
135
136 PATH Determine the search path used during the command search
137 described in Command Search and Execution , except as described
138 under the -p option.
139
140
142 Default.
143
145 When the -v option is specified, standard output shall be formatted as:
146
147
148 "%s\n", <pathname or command>
149
150 When the -V option is specified, standard output shall be formatted as:
151
152
153 "%s\n", <unspecified>
154
156 The standard error shall be used only for diagnostic messages.
157
159 None.
160
162 None.
163
165 When the -v or -V options are specified, the following exit values
166 shall be returned:
167
168 0 Successful completion.
169
170 >0 The command_name could not be found or an error occurred.
171
172
173 Otherwise, the following exit values shall be returned:
174
175 126 The utility specified by command_name was found but could not be
176 invoked.
177
178 127 An error occurred in the command utility or the utility speci‐
179 fied by command_name could not be found.
180
181
182 Otherwise, the exit status of command shall be that of the simple com‐
183 mand specified by the arguments to command.
184
186 Default.
187
188 The following sections are informative.
189
191 The order for command search allows functions to override regular
192 built-ins and path searches. This utility is necessary to allow func‐
193 tions that have the same name as a utility to call the utility (instead
194 of a recursive call to the function).
195
196 The system default path is available using getconf; however, since get‐
197 conf may need to have the PATH set up before it can be called itself,
198 the following can be used:
199
200
201 command -p getconf _CS_PATH
202
203 There are some advantages to suppressing the special characteristics of
204 special built-ins on occasion. For example:
205
206
207 command exec > unwritable-file
208
209 does not cause a non-interactive script to abort, so that the output
210 status can be checked by the script.
211
212 The command, env, nohup, time, and xargs utilities have been specified
213 to use exit code 127 if an error occurs so that applications can dis‐
214 tinguish "failure to find a utility" from "invoked utility exited with
215 an error indication". The value 127 was chosen because it is not com‐
216 monly used for other meanings; most utilities use small values for
217 "normal error conditions" and the values above 128 can be confused with
218 termination due to receipt of a signal. The value 126 was chosen in a
219 similar manner to indicate that the utility could be found, but not
220 invoked. Some scripts produce meaningful error messages differentiating
221 the 126 and 127 cases. The distinction between exit codes 126 and 127
222 is based on KornShell practice that uses 127 when all attempts to exec
223 the utility fail with [ENOENT], and uses 126 when any attempt to exec
224 the utility fails for any other reason.
225
226 Since the -v and -V options of command produce output in relation to
227 the current shell execution environment, command is generally provided
228 as a shell regular built-in. If it is called in a subshell or separate
229 utility execution environment, such as one of the following:
230
231
232 (PATH=foo command -v)
233 nohup command -v
234
235 it does not necessarily produce correct results. For example, when
236 called with nohup or an exec function, in a separate utility execution
237 environment, most implementations are not able to identify aliases,
238 functions, or special built-ins.
239
240 Two types of regular built-ins could be encountered on a system and
241 these are described separately by command. The description of command
242 search in Command Search and Execution allows for a standard utility to
243 be implemented as a regular built-in as long as it is found in the
244 appropriate place in a PATH search. So, for example, command -v true
245 might yield /bin/true or some similar pathname. Other implementation-
246 defined utilities that are not defined by this volume of
247 IEEE Std 1003.1-2001 might exist only as built-ins and have no pathname
248 associated with them. These produce output identified as (regular)
249 built-ins. Applications encountering these are not able to count on
250 execing them, using them with nohup, overriding them with a different
251 PATH , and so on.
252
254 1. Make a version of cd that always prints out the new working direc‐
255 tory exactly once:
256
257
258 cd() {
259 command cd "$@" >/dev/null
260 pwd
261 }
262
263 2. Start off a "secure shell script" in which the script avoids being
264 spoofed by its parent:
265
266
267 IFS='
268 # The preceding value should be <space><tab><newline>.
269 # Set IFS to its default value.
270
271
272 \unalias -a
273 # Unset all possible aliases.
274 # Note that unalias is escaped to prevent an alias
275 # being used for unalias.
276
277
278 unset -f command
279 # Ensure command is not a user function.
280
281
282 PATH="$(command -p getconf _CS_PATH):$PATH"
283 # Put on a reliable PATH prefix.
284
285
286 # ...
287
288 At this point, given correct permissions on the directories called by
289 PATH , the script has the ability to ensure that any utility it calls
290 is the intended one. It is being very cautious because it assumes that
291 implementation extensions may be present that would allow user func‐
292 tions to exist when it is invoked; this capability is not specified by
293 this volume of IEEE Std 1003.1-2001, but it is not prohibited as an
294 extension. For example, the ENV variable precedes the invocation of
295 the script with a user start-up script. Such a script could define
296 functions to spoof the application.
297
299 Since command is a regular built-in utility it is always found prior to
300 the PATH search.
301
302 There is nothing in the description of command that implies the command
303 line is parsed any differently from that of any other simple command.
304 For example:
305
306
307 command a | b ; c
308
309 is not parsed in any special way that causes '|' or ';' to be treated
310 other than a pipe operator or semicolon or that prevents function
311 lookup on b or c.
312
313 The command utility is somewhat similar to the Eighth Edition shell
314 builtin command, but since command also goes to the file system to
315 search for utilities, the name builtin would not be intuitive.
316
317 The command utility is most likely to be provided as a regular built-
318 in. It is not listed as a special built-in for the following reasons:
319
320 * The removal of exportable functions made the special precedence of a
321 special built-in unnecessary.
322
323 * A special built-in has special properties (see Special Built-In
324 Utilities ) that were inappropriate for invoking other utilities.
325 For example, two commands such as:
326
327
328 date > unwritable-file
329
330 command date > unwritable-file
331
332 would have entirely different results; in a non-interactive script, the
333 former would continue to execute the next command, the latter would
334 abort. Introducing this semantic difference along with suppressing
335 functions was seen to be non-intuitive.
336
337 The -p option is present because it is useful to be able to ensure a
338 safe path search that finds all the standard utilities. This search
339 might not be identical to the one that occurs through one of the exec
340 functions (as defined in the System Interfaces volume of
341 IEEE Std 1003.1-2001) when PATH is unset. At the very least, this fea‐
342 ture is required to allow the script to access the correct version of
343 getconf so that the value of the default path can be accurately
344 retrieved.
345
346 The command -v and -V options were added to satisfy requirements from
347 users that are currently accomplished by three different historical
348 utilities: type in the System V shell, whence in the KornShell, and
349 which in the C shell. Since there is no historical agreement on how and
350 what to accomplish here, the POSIX command utility was enhanced and the
351 historical utilities were left unmodified. The C shell which merely
352 conducts a path search. The KornShell whence is more elaborate-in addi‐
353 tion to the categories required by POSIX, it also reports on tracked
354 aliases, exported aliases, and undefined functions.
355
356 The output format of -V was left mostly unspecified because human users
357 are its only audience. Applications should not be written to care about
358 this information; they can use the output of -v to differentiate
359 between various types of commands, but the additional information that
360 may be emitted by the more verbose -V is not needed and should not be
361 arbitrarily constrained in its verbosity or localization for applica‐
362 tion parsing reasons.
363
365 None.
366
368 Command Search and Execution , Shell Execution Environment , Special
369 Built-In Utilities , sh , type , the System Interfaces volume of
370 IEEE Std 1003.1-2001, exec
371
373 Portions of this text are reprinted and reproduced in electronic form
374 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
375 -- Portable Operating System Interface (POSIX), The Open Group Base
376 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
377 Electrical and Electronics Engineers, Inc and The Open Group. In the
378 event of any discrepancy between this version and the original IEEE and
379 The Open Group Standard, the original IEEE and The Open Group Standard
380 is the referee document. The original Standard can be obtained online
381 at http://www.opengroup.org/unix/online.html .
382
383
384
385IEEE/The Open Group 2003 COMMAND(P)