获取有关Linux shell内置命令的帮助
Linux 内置命令属于用户 shell 的一部分,本文将告诉你如何识别它们并获取使用它们的帮助。
Linux 内置命令是内置于 shell 中的命令,很像内置于墙中的书架。与标准 Linux 命令存储在 /usr/bin
中的方式不同,你不会找到它们的独立文件,你可能使用过相当多的内置命令,但你不会感觉到它们与 ls
和 pwd
等命令有何不同。
内置命令与其他 Linux 命令一样使用,它们可能要比不属于 shell 的类似命令运行得快一些。Bash 内置命令包括 alias
、export
和 bg
等。
正如你担心的那样,因为内置命令是特定于 shell 的,所以它们不会提供手册页。使用 man
来查看 bg
,你会看到这样的东西:
<span class="pln">$ </span><span class="kwd">man</span><span class="pln"> bg</span>
<span class="typ">No</span><span class="pln"> manual entry </span><span class="kwd">for</span><span class="pln"> bg</span>
判断内置命令的另一个提示是当你使用 which
命令来识别命令的来源时,Bash 不会响应,表示没有与内置命令关联的文件:
<span class="pln">$ which bg</span>
<span class="pln">$</span>
另一方面,如果你的 shell 是 /bin/zsh
,你可能会得到一个更有启发性的响应:
<span class="pun">%</span><span class="pln"> which bg</span>
<span class="pln">bg</span><span class="pun">:</span><span class="pln"> shell built</span><span class="pun">-</span><span class="kwd">in</span><span class="pln"> command</span>
bash 提供了额外的帮助信息,但它是通过使用 help
命令实现的:
<span class="pln">$ help bg</span>
<span class="pln">bg</span><span class="pun">:</span><span class="pln"> bg </span><span class="pun">[</span><span class="pln">job_spec </span><span class="pun">...]</span>
<span class="pln"> </span><span class="typ">Move</span><span class="pln"> jobs to the background</span><span class="pun">.</span>
<span class="pln"> </span><span class="typ">Place</span><span class="pln"> the jobs identified by each JOB_SPEC </span><span class="kwd">in</span><span class="pln"> the background</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> they</span>
<span class="pln"> had been started </span><span class="kwd">with</span><span class="pln"> </span><span class="str">`&'. If JOB_SPEC is not present, the shell's notion</span>
<span class="str"> of the current job is used.</span>
<span class="str"> Exit Status:</span>
<span class="str"> Returns success unless job control is not enabled or an error occurs.</span>
如果你想要查看 bash 提供的所有内置命令的列表,使用 compgen -b
命令。通过管道将命令输出到列中,以获得较好格式的清单。
<span class="pln">$ compgen </span><span class="pun">-</span><span class="pln">b </span><span class="pun">|</span><span class="pln"> column</span>
<span class="pun">.</span><span class="pln"> compgen </span><span class="kwd">exit</span><span class="pln"> let </span><span class="kwd">return</span><span class="pln"> typeset</span>
<span class="pun">:</span><span class="pln"> complete </span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">local</span><span class="pln"> </span><span class="kwd">set</span><span class="pln"> ulimit</span>
<span class="pun">[</span><span class="pln"> compopt </span><span class="kwd">false</span><span class="pln"> logout shift umask</span>
<span class="kwd">alias</span><span class="pln"> </span><span class="kwd">continue</span><span class="pln"> fc mapfile shopt unalias</span>
<span class="pln">bg </span><span class="kwd">declare</span><span class="pln"> fg popd source </span><span class="kwd">unset</span>
<span class="pln">bind dirs getopts </span><span class="kwd">printf</span><span class="pln"> suspend wait</span>
<span class="kwd">break</span><span class="pln"> disown hash pushd </span><span class="kwd">test</span>
<span class="pln">builtin </span><span class="kwd">echo</span><span class="pln"> help </span><span class="kwd">pwd</span><span class="pln"> times</span>
<span class="kwd">caller</span><span class="pln"> enable history read trap</span>
<span class="kwd">cd</span><span class="pln"> </span><span class="kwd">eval</span><span class="pln"> jobs readarray </span><span class="kwd">true</span>
<span class="pln">command </span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">kill</span><span class="pln"> readonly type</span>
如果你使用 help
命令,你将看到一个内置命令列表以及简短描述。但是,这个列表被截断了(以 help
命令结尾):
<span class="pln">$ help</span>
<span class="pln">GNU </span><span class="kwd">bash</span><span class="pun">,</span><span class="pln"> version </span><span class="lit">5.0</span><span class="pun">.</span><span class="lit">3</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)-</span><span class="pln">release </span><span class="pun">(</span><span class="pln">x86_64</span><span class="pun">-</span><span class="pln">pc</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">)</span>
<span class="typ">These</span><span class="pln"> shell commands are </span><span class="kwd">defined</span><span class="pln"> internally</span><span class="pun">.</span><span class="pln"> </span><span class="typ">Type</span><span class="pln"> </span><span class="str">`help' to see this list.</span>
<span class="str">Type `</span><span class="pln">help name</span><span class="str">' to find out more about the function `name'</span><span class="pun">.</span>
<span class="typ">Use</span><span class="pln"> </span><span class="str">`info bash' to find out more about the shell in general.</span>
<span class="str">Use `</span><span class="kwd">man</span><span class="pln"> </span><span class="pun">-</span><span class="pln">k</span><span class="str">' or `info'</span><span class="pln"> to </span><span class="kwd">find</span><span class="pln"> out </span><span class="kwd">more</span><span class="pln"> about commands </span><span class="kwd">not</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">this</span><span class="pln"> </span><span class="kwd">list</span><span class="pun">.</span>
<span class="pln">A star </span><span class="pun">(*)</span><span class="pln"> </span><span class="kwd">next</span><span class="pln"> to a name means that the command </span><span class="kwd">is</span><span class="pln"> disabled</span><span class="pun">.</span>
<span class="pln"> job_spec </span><span class="pun">[&]</span><span class="pln"> history </span><span class="pun">[-</span><span class="pln">c</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">d offset</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> histo</span><span class="pun">></span>
<span class="pln"> </span><span class="pun">((</span><span class="pln"> expression </span><span class="pun">))</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">then</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="kwd">elif</span><span class="pln"> CO</span><span class="pun">></span>
<span class="pln"> </span><span class="pun">.</span><span class="pln"> filename </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span><span class="pln"> jobs </span><span class="pun">[-</span><span class="pln">lnprs</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">jobspec </span><span class="pun">...]</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> jobs </span><span class="pun">-></span>
<span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="kwd">kill</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">s sigspec </span><span class="pun">|</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n signum </span><span class="pun">|</span><span class="pln"> </span><span class="pun">-</span><span class="pln">sigsp</span><span class="pun">></span>
<span class="pln"> </span><span class="pun">[</span><span class="pln"> arg</span><span class="pun">...</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> let arg </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span>
<span class="pln"> </span><span class="pun">[[</span><span class="pln"> expression </span><span class="pun">]]</span><span class="pln"> </span><span class="kwd">local</span><span class="pln"> </span><span class="pun">[</span><span class="pln">option</span><span class="pun">]</span><span class="pln"> name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pln"> </span><span class="pun">...</span>
<span class="pln"> </span><span class="kwd">alias</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pln"> </span><span class="pun">...</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> logout </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
<span class="pln"> bg </span><span class="pun">[</span><span class="pln">job_spec </span><span class="pun">...]</span><span class="pln"> mapfile </span><span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">n count</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">O ori</span><span class="pun">></span>
<span class="pln"> bind </span><span class="pun">[-</span><span class="pln">lpsvPSVX</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">m keymap</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">f filen</span><span class="pun">></span><span class="pln"> popd </span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[+</span><span class="pln">N </span><span class="pun">|</span><span class="pln"> </span><span class="pun">-</span><span class="pln">N</span><span class="pun">]</span>
<span class="pln"> </span><span class="kwd">break</span><span class="pln"> </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> </span><span class="kwd">printf</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">v </span><span class="kwd">var</span><span class="pun">]</span><span class="pln"> format </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span>
<span class="pln"> builtin </span><span class="pun">[</span><span class="pln">shell</span><span class="pun">-</span><span class="pln">builtin </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]]</span><span class="pln"> pushd </span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[+</span><span class="pln">N </span><span class="pun">|</span><span class="pln"> </span><span class="pun">-</span><span class="pln">N </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">dir</span><span class="pun">]</span>
<span class="pln"> </span><span class="kwd">caller</span><span class="pln"> </span><span class="pun">[</span><span class="kwd">expr</span><span class="pun">]</span><span class="pln"> </span><span class="kwd">pwd</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">LP</span><span class="pun">]</span>
<span class="pln"> </span><span class="kwd">case</span><span class="pln"> WORD </span><span class="kwd">in</span><span class="pln"> </span><span class="pun">[</span><span class="pln">PATTERN </span><span class="pun">[|</span><span class="pln"> PATTERN</span><span class="pun">]...)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> read </span><span class="pun">[-</span><span class="pln">ers</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">a </span><span class="kwd">array</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">i</span><span class="pun">></span>
<span class="pln"> </span><span class="kwd">cd</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">L</span><span class="pun">|[-</span><span class="pln">P </span><span class="pun">[-</span><span class="pln">e</span><span class="pun">]]</span><span class="pln"> </span><span class="pun">[-@]]</span><span class="pln"> </span><span class="pun">[</span><span class="kwd">dir</span><span class="pun">]</span><span class="pln"> readarray </span><span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">n count</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">O o</span><span class="pun">></span>
<span class="pln"> command </span><span class="pun">[-</span><span class="pln">pVv</span><span class="pun">]</span><span class="pln"> command </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span><span class="pln"> readonly </span><span class="pun">[-</span><span class="pln">aAf</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pln"> </span><span class="pun">...]</span><span class="pln"> </span><span class="kwd">or</span><span class="pun">></span>
<span class="pln"> compgen </span><span class="pun">[-</span><span class="pln">abcdefgjksuv</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">o option</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-></span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
<span class="pln"> complete </span><span class="pun">[-</span><span class="pln">abcdefgjksuv</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="kwd">pr</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">DEI</span><span class="pun">]</span><span class="pln"> </span><span class="pun">></span><span class="pln"> select NAME </span><span class="pun">[</span><span class="kwd">in</span><span class="pln"> WORDS </span><span class="pun">...</span><span class="pln"> </span><span class="pun">;]</span><span class="pln"> </span><span class="kwd">do</span><span class="pln"> COMMA</span><span class="pun">></span>
<span class="pln"> compopt </span><span class="pun">[-</span><span class="pln">o</span><span class="pun">|+</span><span class="pln">o option</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">DEI</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">name </span><span class="pun">..></span><span class="pln"> </span><span class="kwd">set</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">abefhkmnptuvxBCHP</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">o option</span><span class="pun">-</span><span class="pln">n</span><span class="pun">></span>
<span class="pln"> </span><span class="kwd">continue</span><span class="pln"> </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> shift </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
<span class="pln"> coproc </span><span class="pun">[</span><span class="pln">NAME</span><span class="pun">]</span><span class="pln"> command </span><span class="pun">[</span><span class="pln">redirections</span><span class="pun">]</span><span class="pln"> shopt </span><span class="pun">[-</span><span class="pln">pqsu</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">o</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">optname </span><span class="pun">...]</span>
<span class="pln"> </span><span class="kwd">declare</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">aAfFgilnrtux</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">va</span><span class="pun">></span><span class="pln"> source filename </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span>
<span class="pln"> dirs </span><span class="pun">[-</span><span class="pln">clpv</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[+</span><span class="pln">N</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">N</span><span class="pun">]</span><span class="pln"> suspend </span><span class="pun">[-</span><span class="pln">f</span><span class="pun">]</span>
<span class="pln"> disown </span><span class="pun">[-</span><span class="pln">h</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">ar</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="pln">jobspec </span><span class="pun">...</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> pid </span><span class="pun">.</span><span class="pln"> </span><span class="pun">&</span><span class="pln">lt</span><span class="pun">;</span><span class="pln">p</span><span class="pun">&</span><span class="pln">gt</span><span class="str">'> test [expr]</span>
<span class="str"> echo [-neE] [arg ...] time [-p] pipeline</span>
<span class="str"> enable [-a] [-dnps] [-f filename] [nam> times</span>
<span class="str"> eval [arg ...] trap [-lp] [[arg] signal_spec ...]</span>
<span class="str"> exec [-cl] [-a name] [command [argumen> true</span>
<span class="str"> exit [n] type [-afptP] name [name ...]</span>
<span class="str"> export [-fn] [name[=value] ...] or exp> typeset [-aAfFgilnrtux] [-p] name[=va></span>
<span class="str"> false ulimit [-SHabcdefiklmnpqrstuvxPT] [li></span>
<span class="str"> fc [-e ename] [-lnr] [first] [last] or> umask [-p] [-S] [mode]</span>
<span class="str"> fg [job_spec] unalias [-a] name [name ...]</span>
<span class="str"> for NAME [in WORDS ... ] ; do COMMANDS> unset [-f] [-v] [-n] [name ...]</span>
<span class="str"> for (( exp1; exp2; exp3 )); do COMMAND> until COMMANDS; do COMMANDS; done</span>
<span class="str"> function name { COMMANDS ; } or name (> variables - Names and meanings of som></span>
<span class="str"> getopts optstring name [arg] wait [-fn] [id ...]</span>
<span class="str"> hash [-lr] [-p pathname] [-dt] [name .> while COMMANDS; do COMMANDS; done</span>
<span class="str"> help [-dms] [pattern ...] { COMMANDS ; }</span>
从上面的清单中可以看出,help
命令本身就是内置的。
你可以通过向 help
命令提供你感兴趣的内置命令名称来获取关于它们的更多信息,例如 help dirs
:
<span class="pln">$ help dirs</span>
<span class="pln">dirs</span><span class="pun">:</span><span class="pln"> dirs </span><span class="pun">[-</span><span class="pln">clpv</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[+</span><span class="pln">N</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">N</span><span class="pun">]</span>
<span class="pln"> </span><span class="typ">Display</span><span class="pln"> directory stack</span><span class="pun">.</span>
<span class="pln"> </span><span class="typ">Display</span><span class="pln"> the </span><span class="kwd">list</span><span class="pln"> of currently remembered directories</span><span class="pun">.</span><span class="pln"> </span><span class="typ">Directories</span>
<span class="pln"> </span><span class="kwd">find</span><span class="pln"> their way onto the </span><span class="kwd">list</span><span class="pln"> </span><span class="kwd">with</span><span class="pln"> the </span><span class="str">`pushd' command; you can get</span>
<span class="str"> back up through the list with the `</span><span class="pln">popd</span><span class="str">' command.</span>
<span class="str"> Options:</span>
<span class="str"> -c clear the directory stack by deleting all of the elements</span>
<span class="str"> -l do not print tilde-prefixed versions of directories relative</span>
<span class="str"> to your home directory</span>
<span class="str"> -p print the directory stack with one entry per line</span>
<span class="str"> -v print the directory stack with one entry per line prefixed</span>
<span class="str"> with its position in the stack</span>
<span class="str"> Arguments:</span>
<span class="str"> +N Displays the Nth entry counting from the left of the list</span>
<span class="str"> shown by dirs when invoked without options, starting with</span>
<span class="str"> zero.</span>
<span class="str"> -N Displays the Nth entry counting from the right of the list</span>
<span class="str"> shown by dirs when invoked without options, starting with</span>
<span class="str"> zero.</span>
<span class="str"> Exit Status:</span>
<span class="str"> Returns success unless an invalid option is supplied or an error occurs.</span>
内置命令提供了每个 shell 的大部分功能。你使用的任何 shell 都有一些内置命令,但是如何获取这些内置命令的信息可能因 shell 而异。例如,对于 zsh
,你可以使用 man zshbuiltins
命令获得其内置命令的描述。
<span class="pln">$ </span><span class="kwd">man</span><span class="pln"> zshbuiltins</span>
<span class="pln">ZSHBUILTINS</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="typ">General</span><span class="pln"> </span><span class="typ">Commands</span><span class="pln"> </span><span class="typ">Manual</span><span class="pln"> ZSHBUILTINS</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span>
<span class="pln">NAME</span>
<span class="pln"> zshbuiltins </span><span class="pun">-</span><span class="pln"> zsh built</span><span class="pun">-</span><span class="kwd">in</span><span class="pln"> commands</span>
<span class="pln">SHELL BUILTIN COMMANDS</span>
<span class="pln"> </span><span class="typ">Some</span><span class="pln"> shell builtin commands take options </span><span class="kwd">as</span><span class="pln"> described </span><span class="kwd">in</span><span class="pln"> individual en</span><span class="pun">\</span>
<span class="pln"> tries</span><span class="pun">;</span><span class="pln"> these are often referred to </span><span class="kwd">in</span><span class="pln"> the </span><span class="kwd">list</span><span class="pln"> below </span><span class="kwd">as</span><span class="pln"> </span><span class="str">`flags' to avoid</span>
<span class="str"> confusion with shell options, which may also have an effect on the behav\</span>
<span class="str"> iour of builtin commands. In this introductory section, `</span><span class="pln">option</span><span class="str">' always</span>
<span class="str"> has the meaning of an option to a command that should be familiar to most</span>
<span class="str"> command line users.</span>
<span class="str">…</span>
在这个冗长的手册页中,你将找到一个内置命令列表,其中包含有用的描述,如下摘录中所示: