什么是命令:`command`?

最近我发现命令: command没有手动输入,但帮助显示如下:

 $ help command command: command [-pVv] command [arg ...] Execute a simple command or display information about commands. Runs COMMAND with ARGS suppressing shell function lookup, or display information about the specified COMMANDs. Can be used to invoke commands on disk when a function with the same name exists. Options: -p use a default value for PATH that is guaranteed to find all of the standard utilities -v print a description of COMMAND similar to the `type' builtin -V print a more verbose description of each COMMAND Exit Status: Returns exit status of COMMAND, or failure if COMMAND is not found. 

command -v是替代的吗?

此命令接受哪些参数以及如何/何时使用command

command内置的bash,我们可以看到:

 seth@host:~$ type command command is a shell builtin 

所以我们知道command是由我们的shell提供的,bash。 深入挖掘man bash我们可以看到它的用途:

(来自man bash ):

 command [-pVv] command [arg ...] Run command with args suppressing the normal shell function lookup. Only builtin commands or commands found in the PATH are executed. If the -p option is given, the search for command is performed using a default value for PATH that is guaranteed to find all of the standard utilities. If either the -V or -v option is supplied, a description of command is printed. The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option produces a more verbose description. If the -V or -v option is supplied, the exit status is 0 if command was found, and 1 if not. If neither option is supplied and an error occurred or command cannot be found, the exit status is 127. Otherwise, the exit status of the command builtin is the exit status of command. 

基本上你会使用command绕过“正常函数查找”。 例如,假设你的.bashrc有一个函数:

 function say_hello() { echo 'Hello!' } 

通常,当你在你的终端bash中运行say_hello ,你会在你的.bashrc找到名为say_hello的函数,比如找到一个名为say_hello的应用程序。 使用:

 command say_hello 

使bash绕过其正常的函数查找并直接进入内置函数或$PATH 。 请注意,此函数查找包括别名。 使用command将绕过函数和别名。

如果提供了-p选项,则bash会绕过您的自定义$PATH并使用自己的默认值。

-v-V标志bash打印命令的描述( -v ,long为-V缩写)。

注意:正如souravc在评论中指出的,可以在这里找到一个更简单的方法来查找有关shell内置函数的信息: 如何使`man`适用于shell内置命令和关键字?

这是Bash shell的内置命令。

我在这个内置中看到的唯一优势总结在帮助文本的以下句子中:

 Can be used to invoke commands on disk when a function with the same name exists 

因此,如果要执行程序(保存某些磁盘的二进制文件),并且存在同名的内部shell函数,则可以使用此内置函数调用程序。

是的, command -v将提供与类型相同的结果。

我在Dash shell下也发现了它。

它有两种不同的用途:

一种用法是忽略别名函数 ,并运行PATH中的可执行文件 ,即使存在别名或具有相同名称的函数。

例如,我将使用ls的别名来附加一个/到目录名:

 $ alias ls='ls --classify' $ ls -d . ./ $ command ls -d . . 

在交互式shell中,在命令名称之前使用反斜杠作为替代,更短的语法可能更方便:

 $ \ls -d . . 

另一个用途是查找在使用-v选项未使用命令名时将运行的命令。 它似乎是最便携/ POSIX的变种。

 $ command -v ls alias ls='ls --classify' $ command -v sed /bin/sed 

它允许您运行shell命令,忽略任何shell函数。

http://ss64.com/bash/command.html

command很有用,例如,如果要检查特定命令的存在。 which包括查找中的别名,因此它不适合此目的,因为您不希望将随机别名视为相关命令。

换句话说,你可以在shell脚本中有一个小函数,如下所示:

 exists() { command -v "$1" >/dev/null 2>&1 } 

然后测试一个可用的命令(这里是dialog ),如下所示:

 if ! exists dialog ; then echo "This script requires 'dialog'." echo "Install it with 'sudo apt-get install dialog', then try again!" exit 1 fi