获取有关Linux shell内置命令的帮助

系统 Linux
Linux 内置命令属于用户 shell 的一部分,本文将告诉你如何识别它们并获取使用它们的帮助。

 [[272147]]

Linux 内置命令属于用户 shell 的一部分,本文将告诉你如何识别它们并获取使用它们的帮助。

Linux 内置命令是内置于 shell 中的命令,很像内置于墙中的书架。与标准 Linux 命令存储在 /usr/bin 中的方式不同,你不会找到它们的独立文件,你可能使用过相当多的内置命令,但你不会感觉到它们与 lspwd 等命令有何不同。

内置命令与其他 Linux 命令一样使用,它们可能要比不属于 shell 的类似命令运行得快一些。Bash 内置命令包括 aliasexportbg 等。

正如你担心的那样,因为内置命令是特定于 shell 的,所以它们不会提供手册页。使用 man 来查看 bg,你会看到这样的东西:

  1. $ man bg
  2. No manual entry for bg

判断内置命令的另一个提示是当你使用 which 命令来识别命令的来源时,Bash 不会响应,表示没有与内置命令关联的文件:

  1. $ which bg
  2. $

另一方面,如果你的 shell 是 /bin/zsh,你可能会得到一个更有启发性的响应:

  1. % which bg
  2. bg: shell built-in command

bash 提供了额外的帮助信息,但它是通过使用 help 命令实现的:

  1. $ help bg
  2. bg: bg [job_spec ...]
  3. Move jobs to the background.
  4.  
  5. Place the jobs identified by each JOB_SPEC in the background, as if they
  6. had been started with `&'. If JOB_SPEC is not present, the shell's notion
  7. of the current job is used.
  8.  
  9. Exit Status:
  10. Returns success unless job control is not enabled or an error occurs.

如果你想要查看 bash 提供的所有内置命令的列表,使用 compgen -b 命令。通过管道将命令输出到列中,以获得较好格式的清单。

  1. $ compgen -b | column
  2. . compgen exit let return typeset
  3. : complete export local set ulimit
  4. [ compopt false logout shift umask
  5. alias continue fc mapfile shopt unalias
  6. bg declare fg popd source unset
  7. bind dirs getopts printf suspend wait
  8. break disown hash pushd test
  9. builtin echo help pwd times
  10. caller enable history read trap
  11. cd eval jobs readarray true
  12. command exec kill readonly type

如果你使用 help 命令,你将看到一个内置命令列表以及简短描述。但是,这个列表被截断了(以 help 命令结尾):

  1. $ help
  2. GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
  3. These shell commands are defined internally. Type `help' to see this list.
  4. Type `help name' to find out more about the function `name'.
  5. Use `info bash' to find out more about the shell in general.
  6. Use `man -k' or `info' to find out more about commands not in this list.
  7.  
  8. A star (*) next to a name means that the command is disabled.
  9.  
  10. job_spec [&] history [-c] [-d offset] [n] or histo>
  11. (( expression )) if COMMANDS; then COMMANDS; [ elif CO>
  12. . filename [arguments] jobs [-lnprs] [jobspec ...] or jobs ->
  13. : kill [-s sigspec | -n signum | -sigsp>
  14. [ arg... ] let arg [arg ...]
  15. [[ expression ]] local [option] name[=value] ...
  16. alias [-p] [name[=value] ... ] logout [n]
  17. bg [job_spec ...] mapfile [-d delim] [-n count] [-O ori>
  18. bind [-lpsvPSVX] [-m keymap] [-f filen> popd [-n] [+N | -N]
  19. break [n] printf [-v var] format [arguments]
  20. builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
  21. caller [expr] pwd [-LP]
  22. case WORD in [PATTERN [| PATTERN]...) > read [-ers] [-a array] [-d delim] [-i>
  23. cd [-L|[-P [-e]] [-@]] [dir] readarray [-d delim] [-n count] [-O o>
  24. command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] or>
  25. compgen [-abcdefgjksuv] [-o option] [-> return [n]
  26. complete [-abcdefgjksuv] [-pr] [-DEI] > select NAME [in WORDS ... ;] do COMMA>
  27. compopt [-o|+o option] [-DEI] [name ..> set [-abefhkmnptuvxBCHP] [-o option-n>
  28. continue [n] shift [n]
  29. coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
  30. declare [-aAfFgilnrtux] [-p] [name[=va> source filename [arguments]
  31. dirs [-clpv] [+N] [-N] suspend [-f]
  32. disown [-h] [-ar] [jobspec ... | pid . <p&gt'> test [expr]
  33. echo [-neE] [arg ...] time [-p] pipeline
  34. enable [-a] [-dnps] [-f filename] [nam> times
  35. eval [arg ...] trap [-lp] [[arg] signal_spec ...]
  36. exec [-cl] [-a name] [command [argumen> true
  37. exit [n] type [-afptP] name [name ...]
  38. export [-fn] [name[=value] ...] or exp> typeset [-aAfFgilnrtux] [-p] name[=va>
  39. false ulimit [-SHabcdefiklmnpqrstuvxPT] [li>
  40. fc [-e ename] [-lnr] [first] [last] or> umask [-p] [-S] [mode]
  41. fg [job_spec] unalias [-a] name [name ...]
  42. for NAME [in WORDS ... ] ; do COMMANDS> unset [-f] [-v] [-n] [name ...]
  43. for (( exp1; exp2; exp3 )); do COMMAND> until COMMANDS; do COMMANDS; done
  44. function name { COMMANDS ; } or name (> variables - Names and meanings of som>
  45. getopts optstring name [arg] wait [-fn] [id ...]
  46. hash [-lr] [-p pathname] [-dt] [name .> while COMMANDS; do COMMANDS; done
  47. help [-dms] [pattern ...] { COMMANDS ; }

从上面的清单中可以看出,help 命令本身就是内置的。

你可以通过向 help 命令提供你感兴趣的内置命令名称来获取关于它们的更多信息,例如 help dirs

  1. $ help dirs
  2. dirs: dirs [-clpv] [+N] [-N]
  3. Display directory stack.
  4.  
  5. Display the list of currently remembered directories. Directories
  6. find their way onto the list with the `pushd' command; you can get
  7. back up through the list with the `popd' command.
  8.  
  9. Options:
  10. -c clear the directory stack by deleting all of the elements
  11. -l do not print tilde-prefixed versions of directories relative
  12. to your home directory
  13. -p print the directory stack with one entry per line
  14. -v print the directory stack with one entry per line prefixed
  15. with its position in the stack
  16.  
  17. Arguments:
  18. +N Displays the Nth entry counting from the left of the list
  19. shown by dirs when invoked without options, starting with
  20. zero.
  21.  
  22. -N Displays the Nth entry counting from the right of the list
  23. shown by dirs when invoked without options, starting with
  24. zero.
  25.  
  26. Exit Status:
  27. Returns success unless an invalid option is supplied or an error occurs.

内置命令提供了每个 shell 的大部分功能。你使用的任何 shell 都有一些内置命令,但是如何获取这些内置命令的信息可能因 shell 而异。例如,对于 zsh,你可以使用 man zshbuiltins 命令获得其内置命令的描述。

  1. $ man zshbuiltins
  2.  
  3. ZSHBUILTINS(1) General Commands Manual ZSHBUILTINS(1)
  4.  
  5. NAME
  6. zshbuiltins - zsh built-in commands
  7.  
  8. SHELL BUILTIN COMMANDS
  9. Some shell builtin commands take options as described in individual en
  10. tries; these are often referred to in the list below as `flags' to avoid
  11. confusion with shell options, which may also have an effect on the behav‐
  12. iour of builtin commands. In this introductory section, `option' always
  13. has the meaning of an option to a command that should be familiar to most
  14. command line users.

在这个冗长的手册页中,你将找到一个内置命令列表,其中包含有用的描述,如下摘录中所示:

  1. bg [ job ... ]
  2. job ... &
  3. Put each specified job in the background, or the current job if
  4. none is specified.
  5.  
  6. bindkey
  7. See the section `Zle Builtins' in zshzle(1).
  8.  
  9. break [ n ]
  10. Exit from an enclosing for, while, until, select or repeat loop.
  11. If an arithmetic expression n is specified, then break n levels
  12. instead of just one.

最后

Linux 内置命令对于每个 shell 都很重要,它的操作类似特定于 shell 的命令一样。如果你经常使用不同的 shell,并注意到你经常使用的某些命令似乎不存在或者不能按预期工作,那么它可能是你使用的其他 shell 之一中的内置命令。

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2018-02-24 15:40:06

LinuxBashShell

2010-03-02 09:14:00

Linux创建用户命令

2019-12-01 22:59:43

Linux shell命令进程

2014-02-20 11:31:34

last命令审计工具

2014-07-31 11:24:21

Linuxshell命令

2011-01-18 13:53:42

Linux Shell命令

2010-06-23 17:34:03

Linux Bash

2010-06-23 14:45:02

Linux Bash

2010-03-23 15:24:45

Linux shell

2010-06-23 17:37:14

Linux Bash

2022-12-12 09:12:30

2009-12-25 09:47:05

LinuxShell编程bash

2014-09-17 10:20:20

Linuxshell

2017-09-04 16:20:38

Linuxshell命令

2019-10-24 07:57:37

Linuxshell获取时间

2010-06-23 17:16:33

Linux Bash

2010-06-23 17:29:07

Linux Bash

2010-06-23 16:31:10

Linux Bash

2009-12-18 08:57:27

Linux shell

2013-09-18 10:21:20

命令Shell
点赞
收藏

51CTO技术栈公众号