📄 command.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>command</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_001_014_149"> </a>NAME</h4><blockquote>command - execute a simple command</blockquote><h4><a name = "tag_001_014_150"> </a>SYNOPSIS</h4><blockquote><pre><code>command <b>[</b>-p<b>] </b><i>command_name </i><b>[</b><i>argument</i> ...<b>]</b>command <b>[</b> -v | -V <b>] </b><i>command_name</i></code></pre></blockquote><h4><a name = "tag_001_014_151"> </a>DESCRIPTION</h4><blockquote>The<i>command</i>utility causes the shell to treat thearguments as a simple command, suppressing theshell function lookup that isdescribed in<xref href=cmdsea><a href="chap2.html#tag_001_009_001_001">Command Search and Execution</a></xref>item 1b.<p>If the<i>command_name</i>is the same as the name of one of the special built-in utilities,the special properties in the enumerated list at the beginning of<xref href=sbi><a href="chap2.html#tag_001_014">Special Built-in Utilities</a></xref>will not occur.In every other respect, if<i>command_name</i>is not the name of a function, the effect of<i>command</i>will be the same as omitting<i>command</i>.<p>The<i>command</i>utility also provides information concerninghow a command name will be interpreted by the shell;see<b>-v</b>and<b>-V</b>.</blockquote><h4><a name = "tag_001_014_152"> </a>OPTIONS</h4><blockquote>The<i>command</i>utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b> </a> .<p>The following options are supported:<dl compact><dt><b>-p</b><dd>Perform the command search using a default value for<i>PATH</i>that is guaranteed to find all of the standard utilities.<dt><b>-v</b><dd>Write a string to standard output that indicatesthe pathname or command that will be used by theshell, in the current shell execution environment (see<xref href=shexenv><a href="chap2.html#tag_001_012">Shell Execution Environment</a></xref>),to invoke<i>command_name</i>.<ul><li>Utilities, regular built-in utilities,<i>command_names</i>including a slash character,and any implementation-dependent functions that are found using the<i>PATH</i>variable (as described in<xref href=cmdsea><a href="chap2.html#tag_001_009_001_001">Command Search and Execution</a></xref>),will be written as absolute pathnames.<li>Shell functions, special built-in utilities,regular built-in utilities not associated with a<i>PATH</i>search,and shell reserved wordswill be written as just their names.<li>An alias will be written as a command linethat represents its alias definition.<li>Otherwise, no output will be writtenand the exit status will reflectthat the name was not found.</ul><dt><b>-V</b><dd>Write a string to standard outputthat indicates how the name given in the<i>command_name</i>operand will be interpreted by theshell, in the current shell execution environment (see<xref href=shexenv><a href="chap2.html#tag_001_012">Shell Execution Environment</a></xref>).Although the format of this string is unspecified,it will indicate in whichof the following categories<i>command_name</i>falls and include the information stated:<ul><li>Utilities, regular built-in utilities,and any implementation-dependent functionsthat are found using the<i>PATH</i>variable (as described in<xref href=cmdsea><a href="chap2.html#tag_001_009_001_001">Command Search and Execution</a></xref>),will be identified as such andinclude the absolute pathname in the string.<li>Other shell functions will be identified as functions.<li>Aliases will be identified as aliases andtheir definitions will be included in the string.<li>Special built-in utilities will be identified as special built-in utilities.<li>Regular built-in utilities not associated with a<i>PATH</i>search will be identified as regular built-in utilities.(The term "regular" need not be used.)<li>Shell reserved words will be identified as reserved words.</ul></dl></blockquote><h4><a name = "tag_001_014_153"> </a>OPERANDS</h4><blockquote>The following operands are supported:<dl compact><dt><i>argument</i><dd>One of the strings treated as an argument to<i>command_name</i>.<dt><i>command_name</i><dd>The name of a utility or a special built-in utility.</dl></blockquote><h4><a name = "tag_001_014_154"> </a>STDIN</h4><blockquote>Not used.</blockquote><h4><a name = "tag_001_014_155"> </a>INPUT FILES</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_156"> </a>ENVIRONMENT VARIABLES</h4><blockquote>The following environment variables affect the execution of<i>command</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text data ascharacters (for example, single- as opposed to multi-byte charactersin arguments).<dt><i>LC_MESSAGES</i><dd>Determine the locale that should be used to affectthe format and contents of diagnosticmessages written to standard errorand informative messages written to standard output.<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i><dt><i>PATH</i><dd>Determine the search pathused during the command search described in<xref href=cmdsea><a href="chap2.html#tag_001_009_001_001">Command Search and Execution</a></xref>,except as described under the<b>-p</b>option.</dl></blockquote><h4><a name = "tag_001_014_157"> </a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_158"> </a>STDOUT</h4><blockquote>When the<b>-v</b>option is specified,standard output is formatted as:<p><code><tt>"%s\n"</tt>, <<i>pathname or command</i>></code><p>When the<b>-V</b>option is specified,standard output is formatted as:<p><code><tt>"%s\n"</tt>, <<i>unspecified</i>></code></blockquote><h4><a name = "tag_001_014_159"> </a>STDERR</h4><blockquote>Used only for diagnostic messages.</blockquote><h4><a name = "tag_001_014_160"> </a>OUTPUT FILES</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_161"> </a>EXTENDED DESCRIPTION</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_162"> </a>EXIT STATUS</h4><blockquote>When the<b>-v</b>or<b>-V</b>options are specified, the following exit values are returned:<dl compact><dt>0<dd>Successful completion.<dt>>0<dd>The<i>command_name</i>could not be found or an error occurred.</dl><p>Otherwise, the following exit values are returned:<dl compact><dt>126<dd>The utility specified by<i>command_name</i>was found but could not be invoked.<dt>127<dd>An error occurred in the<i>command</i>utility or the utility specified by<i>command_name</i>could not be found.</dl><p>Otherwise, the exit status of<i>command</i>will be that of the simple command specified by the arguments to<i>command</i>.</blockquote><h4><a name = "tag_001_014_163"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_164"> </a>APPLICATION USAGE</h4><blockquote>The order for command searchallows functions to override regular built-ins and path searches.This utility is necessary to allow functions that have the same name asa utility to call the utility (instead of a recursive call to the function).<p>The system default path is available using<i><a href="getconf.html">getconf</a></i>;however, since<i><a href="getconf.html">getconf</a></i>may need to have the<i>PATH</i>set up before it can be called itself, the following can be used:<pre><code>command -p getconf _CS_PATH</code></pre><p>There are some advantages to suppressing thespecial characteristics of special built-ins on occasion.For example:<pre><code>command exec > <i>unwritable-file</i></code></pre>will not cause a non-interactive script to abort, so that theoutput status can be checked by the script.<p>The<i>command</i>,<i><a href="env.html">env</a></i>,<i><a href="nohup.html">nohup</a></i>,<i><a href="time.html">time</a></i>and<i><a href="xargs.html">xargs</a></i>utilities have been specified to useexit code 127 if an error occurs so thatapplications can distinguish"failure to find a utility" from "invoked utility exitedwith an error indication".The value 127 was chosen because it is not commonly used for other meanings;most utilities use small values for "normal error conditions" andthe values above 128 can be confused with termination due to receipt of asignal.The value 126was chosen in a similar manner to indicate that the utilitycould be found, but not invoked.Some scripts produce meaningful error messagesdifferentiating the 126 and 127 cases.The distinction between exit codes 126 and 127 is basedon KornShell practice that uses 127 when all attempts to<i>exec</i>the utility fail with[ENOENT],and uses 126 when any attempt to<i>exec</i>the utility fails for any other reason.<p>Since the<b>-v</b>and<b>-V</b>options of<i>command</i>produce output in relation to thecurrent shell execution environment,<i>command</i>is generally provided as a shell regular built-in.If it is called in a subshell or separate utility execution environment,such as one of the following:<pre><code>(PATH=foo command -v) nohup command -v</code></pre>it will not necessarily produce correct results.For example, when called with<i><a href="nohup.html">nohup</a></i>or an<i>exec</i>function, in a separate utility execution environment,most implementations will not be able to identifyaliases, functions or special built-ins.<p>Two types of regular built-ins could be encounteredon a system and these are described separately by<i>command</i>.The description of command search in<xref href=cmdsea><a href="chap2.html#tag_001_009_001_001">Command Search and Execution</a></xref>allows for a standard utility to be implementedas a regular built-in as long as it is foundin the appropriate place in a<i>PATH</i>search.So, for example,<i>command</i><b>-v</b>truemight yield<b>/bin/true</b>or some similar pathname.Other implementation-dependent utilitiesthat are not defined by this specificationmight exist only as built-ins and have nopathname associated with them.These will produce output identified as (regular) built-ins.Applications encountering these will not be ableto count on<i>exec</i>ingthem, using them with<i><a href="nohup.html">nohup</a></i>,overriding them with a different<i>PATH ,</i>and so on.<br></blockquote><h4><a name = "tag_001_014_165"> </a>EXAMPLES</h4><blockquote><ol><p><li>Make a version of<i><a href="cd.html">cd</a></i>that always prints out the new working directory exactly once:<pre><code>cd() { command cd "$@" >/dev/null pwd}</code></pre><p><li>Start off a "secure shell script" in which the script avoidsbeing spoofed by its parent:<pre><code>IFS=''# The preceding value should be <space><tab><newline>.# Set IFS to its default value.\unalias -a# Unset all possible aliases.# Note that unalias is escaped to prevent an alias# being used for unalias.unset -f command# Ensure command is not a user function.PATH="$(command -p getconf _CS_PATH):$PATH"# Put on a reliable PATH prefix.# ...</code></pre><p>At this point, given correct permissions on the directories called by<i>PATH ,</i>the script has the ability to ensure that any utility itcalls is the intended one.It is being very cautious because it assumes thatimplementation extensions may be present that wouldallow user functions to exist whenit is invoked; this capability is not specified by this specification,but it is not prohibited as an extension.For example, the<i>ENV</i>variable precedes the invocation of the scriptwith a user startup script.Such a script could define functionsto spoof the application.<p></ol></blockquote><h4><a name = "tag_001_014_166"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_167"> </a>SEE ALSO</h4><blockquote><i><a href="sh.html">sh</a></i>,<i><a href="type.html">type</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -