列出 bash 中特定命令的所有可选参数

abh*_*h30 8 command-line bash

我想知道是否有一种方法可以在 bash 中找出命令的所有可选参数,而无需打开手册页并查看大量不需要的信息。

Eg:
Input: <some_command> cat
Output: -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit
Run Code Online (Sandbox Code Playgroud)

ste*_*ver 12

它绝不是通用的,但许多命令提供了一个使用摘要来响应 a-h--help参数,例如

$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'
Run Code Online (Sandbox Code Playgroud)

  • 在某些情况下,尽管 `--help` 是一个无效选项,但无论如何都会返回选项。例如,`lspci --help` (2认同)

Eli*_*gan 5

最佳方法取决于您正在运行的命令类型。您可以尝试-h--help按照steeldriver 的建议进行尝试,但对于某些可能无法显示所有可用选项的命令。理想情况下,将显示所有选项;即便如此,它们也可能无法全部得到解释。官方文档,例如 amaninfopage,通常会显示和解释所有选项——或者至少是开发人员希望用户实际使用的所有选项。(有时确实存在未记录的选项。)

首先,我建议检查该命令是外部命令还是shell 内置命令。您可以使用type命令执行此操作。在 中bash,您可以将-a标志传递给type命令,以显示如果第一个匹配项不存在时运行的内容:

ek@Io:~$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
Run Code Online (Sandbox Code Playgroud)

对我(也可能是你)来说,ls是一个别名。别名扩展是非递归的,因此lsinls --color=auto不使用别名,而是使用第二个列表 , /bin/ls。这是一个路径到一个可执行文件,因此,外部命令。

因此,要查看有关ls包括所有可用选项的文档,我会运行man ls.

有些情况,例如cat,很简单:

ek@Io:~$ type -a cat
cat is /bin/cat
Run Code Online (Sandbox Code Playgroud)

shell 中内置的命令通常没有自己的手册页,但您可以使用help内置命令来了解它们:

ek@Io:~$ type -a history
history is a shell builtin
ek@Io:~$ type -a help
help is a shell builtin
Run Code Online (Sandbox Code Playgroud)

因此,您可以运行help history以了解historyhelp help了解help

对于某些内置函数,例如compgenhelp内置函数向您展示了它们的所有选项,但并没有解释其中的大部分。每当您需要有关 shell 内置函数的更多信息时,您可以查阅bash( man bash)的手册页,或者您可以通过运行info bash在线访问来查阅更长、更完整的文档。您可能会发现有关内置函数的部分特别有用。

一些命令既可用作外部可执行文件,也可用作 shell 内置命令,这是使用type -afirst 可能会有所帮助的另一个原因:

ek@Io:~$ type -a printf
printf is a shell builtin
printf is /usr/bin/printf
Run Code Online (Sandbox Code Playgroud)

man printf例如,如果您运行,您将不会看到有关该-v选项的任何信息,因为/usr/bin/printf不支持它。该bashshell内建做,不过,当你运行显示的文本help printf列出并解释它。

最后,对于联机帮助页,知道您可以在线阅读它们很有用。