命令的用途是什么:`command`?

Pan*_*dya 58 bash execute-command

最近我发现 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.
Run Code Online (Sandbox Code Playgroud)

command -v的替代品which

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

Set*_*eth 68

command正如我们所见,是一个内置的 bash :

seth@host:~$ type command
command is a shell builtin
Run Code Online (Sandbox Code Playgroud)

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

(来自man bash):

seth@host:~$ type command
command is a shell builtin
Run Code Online (Sandbox Code Playgroud)

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

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.  
Run Code Online (Sandbox Code Playgroud)

通常,当您say_hello在终端中运行时,bash 会找到say_hello.bashrc 之前命名的函数,例如,一个名为say_hello. 使用:

command say_hello  
Run Code Online (Sandbox Code Playgroud)

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

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

-v-Vbash的打印的说明(以下简称标志-v,长-V)的命令的。

注意:正如 souravc 在评论中指出的,可以在此处找到一种更简单的查找有关shell 内置命令的信息的方法:如何使 `man` 为 shell 内置命令和关键字工作?

  • 尝试`sudo apt-get install manpages-posix`。默认情况下不安装它。看 [这里](http://askubuntu.com/a/439752/127327) (2认同)

Ben*_*oit 15

这是 Bash shell 的内置命令。

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

当存在同名函数时,可用于调用磁盘上的命令。

因此,如果您想执行一个程序(保存在磁盘某处的二进制文件),并且存在同名的内部 shell 函数,那么您可以使用这个内置函数来调用您的程序。

是的,command -v将给出与type.

我也在 Dash shell 下找到了它。

  • 值得更明确地添加的是,尽管 `command (name)` 会忽略 shell 函数,但 `command -v (name)` 不会。`command -v (name) >/dev/null` 应该是检查具有该名称的命令是否存在的可移植方式,无论它是 shell 内置、函数还是外部实用程序。 (2认同)

Vol*_*gel 10

它有两种不同的用途:

One use is to ignore aliases and functions, and run the executable file found in PATH, even when an alias or a function with the same name exists.

As example, I'll use an alias for ls that appends a / to directory names:

$ alias ls='ls --classify'
$ ls -d .
./
$ command ls -d .
.
Run Code Online (Sandbox Code Playgroud)

In an interactive shell, it may be more convenient to use a backslash before the command name as alternative, shorter syntax:

$ \ls -d .
.
Run Code Online (Sandbox Code Playgroud)

The other use is to find the command that will be run when the commands name isn't used by using the option -v. It seems to be the most portable/POSIX variant of which.

$ command -v ls
alias ls='ls --classify'
$ command -v sed
/bin/sed
Run Code Online (Sandbox Code Playgroud)