AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 512770
Accepted
Pandya
Pandya
Asked: 2014-08-18 22:02:45 +0800 CST2014-08-18 22:02:45 +0800 CST 2014-08-18 22:02:45 +0800 CST

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

  • 772

最近发现command:command没有手动输入但是help显示如下:

$ 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替代品which吗?

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

bash
  • 6 6 个回答
  • 32957 Views

6 个回答

  • Voted
  1. Best Answer
    Seth
    2014-10-19T19:43:13+08:002014-10-19T19:43:13+08:00

    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!'
    }
    

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

    command say_hello  
    

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

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

    -vor flags bash 打印命令的-V描述(简称-v,长-V)。

    注意:正如 souravc 在评论中指出的,可以在此处找到一种更简单的查找有关 shell 内置命令信息的方法:How to make `man` work for shell builtin commands and keywords?

    • 80
  2. Benoit
    2014-08-18T23:11:37+08:002014-08-18T23:11:37+08:00

    这是 Bash shell 的内置命令。

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

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

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

    是的,command -v将给出与 相同类型的结果type。

    我也在 Dash shell 下找到了它。

    • 16
  3. Volker Siegel
    2014-10-20T03:23:51+08:002014-10-20T03:23:51+08:00

    它有两种不同的用途:

    一种用途是忽略别名和函数,并运行在 PATH 中找到的可执行文件,即使存在别名或同名函数也是如此。

    例如,我将使用别名ls将 a 附加/到目录名称:

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

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

    $ \ls -d .
    .
    

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

    $ command -v ls
    alias ls='ls --classify'
    $ command -v sed
    /bin/sed
    
    • 12
  4. Ville
    2018-02-12T15:38:55+08:002018-02-12T15:38:55+08:00

    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
    
    • 4
  5. Paul Tanzini
    2014-10-19T19:42:48+08:002014-10-19T19:42:48+08:00

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

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

    • 3
  6. jxramos
    2022-06-07T18:08:28+08:002022-06-07T18:08:28+08:00

    我已经看到用作将命令行参数传递给需要作为后台进程运行的可执行文件的技术。这是网上找的例子

    command ping -i 5 google.com &

    https://www.digitalocean.com/community/tutorials/how-to-use-bash-s-job-control-to-manage-foreground-and-background-processes#starting-processes

    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve