help
显示有关内置命令的信息。帮助的来源是什么?它是否维护内置命令的数据库,或者它是否读取每个内置命令的一些文件(类似于每个实用程序的联机帮助页)?
有时我发现它的信息似乎扩展了 --help
$ cd --help
bash: cd: --: invalid option
cd: usage: cd [-L|[-P [-e]]] [dir]
$ help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
Run Code Online (Sandbox Code Playgroud)
我想help
提取可执行文件的帮助部分,但对于 python 脚本pdf-merge.py,它不是
$ help ./pdf-merge.py
bash: help: no help topics match `./pdf-merge.py'. Try `help help' or `man -k ./pdf-merge.py' or `info ./pdf-merge.py'.
$ ./pdf-merge.py --help
usage: pdf-merge.py [-h] [-v] [--ask] [--output OUTPUT] [--title TITLE]
[--author AUTHOR] [--keyword KEYWORD] [--pdftk PDFTK]
[--gs GS] [--pdfmarks PDFMARKS] [--unicode]
PDF [PDF ...]
Merge PDFs preserving bookmarks. Thanks to Larry Cai for suggesting that
Unicode be supported and for discussion about the `--pdfmarks` option.
positional arguments:
PDF an input PDF to merge
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
--ask pause for manual pdfmark tweaking
--output OUTPUT name of the output PDF
--title TITLE title of output PDF
--author AUTHOR author of output PDF
--keyword KEYWORD keywords for the output PDF
--pdftk PDFTK path to the pdftk executable
--gs GS path to the gs (Ghostscript) executable
--pdfmarks PDFMARKS path to pdfmarks file. If not given, a temporary file
is used. If given and the file is missing, execution
will stop after the file is created (before the
Ghostscript run). If given and the file exists, no
attempt will be make to use pdftk to generate the mark
file (I assume your input file is what you want).
--unicode instead of merging PDFs, convert PDF-formatted unicode
strings. For example `--unicode '<FEFF03B103B203B3>'
\u03b1\u03b2\u03b3`
Run Code Online (Sandbox Code Playgroud)
gol*_*cks 12
有时我发现它的信息似乎扩展了 --help
help cd
并且cd --help
是根本不同的。help
是一个内置于 shell 中的命令,它提供有关内置于 shell 中的其他命令的信息,这意味着它们不是自己的可执行文件,它们是例如bash
. 这可能会让人有点困惑,因为一些内置命令也有独立的可执行版本。在这种情况下,他们通常有自己的手册页,如果你问,他们会公开一个可执行路径which [command]
。手册页或 from[command] --help
中的信息用于可执行文件;来自help [command]
内置的信息,但希望它们或多或少相同。如果您寻找一个仅内置命令的手册页,您可能会得到一个列出所有内置命令的 shell 页面。
--help
(包括缩写形式-h
)只是可执行文件的命令行选项的常规标签。许多(但不是全部)CLI 工具实现了这一点,但它们不受约束,并且提供的信息完全取决于实现。如果您调用--help
内置的 shell,您可能会收到“无效选项”和简短的“用法”消息。如果您在未实现它的独立设备上调用它,您可能还会得到一个“无效选项”,但究竟会发生什么取决于应用程序。
如果有可用的命令的内置和独立版本,并且您想知道在调用它时使用的是哪个版本,则可以使用type
另一个内置的 shell。
> help type
type: type [-afptP] name [name ...]
Display information about command type.
For each NAME, indicate how it would be interpreted if used as a
command name.
[...]
> which echo
/bin/echo
> type echo
echo is a shell builtin
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到,虽然有一个独立的可执行文件echo
,但echo
你的 shell 调用的是一个内置的。
小智 8
您已经回答了自己的问题:
nicolas@host:~$ help help
help: help [-s] [pattern ...]
Display helpful information about builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise a list of the builtins is printed. The -s option
restricts the output for each builtin command matching PATTERN to
a short usage synopsis.
Run Code Online (Sandbox Code Playgroud)
Help 是一个内置命令(意思是 bash 内部命令),用于获取其他内置命令的信息。由于这第三部分脚本不是 bash 的内置命令。如果你运行bash
,调用内置help
的使用strace
,您将得到:
# strace bash -i -c "help cd"
---snip(long output)---
write(1, "cd: cd [-L|-P] [dir]\n"..., 21cd: cd [-L|-P] [dir]
) = 21
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2570, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f38b765c000
read(3, "# Locale name alias data base.\n# "..., 4096) = 2570
read(3, ""..., 4096) = 0
close(3) = 0
munmap(0x7f38b765c000, 4096) = 0
open("/usr/share/locale/pt_BR/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/pt/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(1, " Change the current directory "..., 710 Change the current directory to DIR. The variable $HOME is the
default DIR. The variable CDPATH defines the search path for
the directory containing DIR. Alternative directory names in CDPATH
are separated by a colon (:). A null directory name is the same as
the current directory, i.e. `.'. If DIR begins with a slash (/),
then CDPATH is not used. If the directory is not found, and the
shell option `cdable_vars' is set, then try the word as a variable
name. If that variable has a value, then cd to the value of that
variable. The -P option says to use the physical directory structure
instead of following symbolic links; the -L option forces symbolic links
) = 710
write(1, " to be followed.\n"..., 20 to be followed.
) = 20
---snip(long output)---
Run Code Online (Sandbox Code Playgroud)
几乎意味着这些信息是在构建时在 bash 二进制文件中生成的。