獲取有關 Linux shell 內建命令的幫助
Linux 內建命令屬於使用者 shell 的一部分,本文將告訴你如何識別它們並獲取使用它們的幫助。
Linux 內建命令是內建於 shell 中的命令,很像內建於牆中的書架。與標準 Linux 命令儲存在 /usr/bin
中的方式不同,你不會找到它們的獨立檔案,你可能使用過相當多的內建命令,但你不會感覺到它們與 ls
和 pwd
等命令有何不同。
內建命令與其他 Linux 命令一樣使用,它們可能要比不屬於 shell 的類似命令執行得快一些。Bash 內建命令包括 alias
、export
和 bg
等。
正如你擔心的那樣,因為內建命令是特定於 shell 的,所以它們不會提供手冊頁。使用 man
來檢視 bg
,你會看到這樣的東西:
$ man bg
No manual entry for bg
判斷內建命令的另一個提示是當你使用 which
命令來識別命令的來源時,Bash 不會響應,表示沒有與內建命令關聯的檔案:
$ which bg
$
另一方面,如果你的 shell 是 /bin/zsh
,你可能會得到一個更有啟發性的響應:
% which bg
bg: shell built-in command
bash 提供了額外的幫助資訊,但它是通過使用 help
命令實現的:
$ help bg
bg: bg [job_spec ...]
Move jobs to the background.
Place the jobs identified by each JOB_SPEC in the background, as if they
had been started with `&'. If JOB_SPEC is not present, the shell's notion
of the current job is used.
Exit Status:
Returns success unless job control is not enabled or an error occurs.
如果你想要檢視 bash 提供的所有內建命令的列表,使用 compgen -b
命令。通過管道將命令輸出到列中,以獲得較好格式的清單。
$ compgen -b | column
. compgen exit let return typeset
: complete export local set ulimit
[ compopt false logout shift umask
alias continue fc mapfile shopt unalias
bg declare fg popd source unset
bind dirs getopts printf suspend wait
break disown hash pushd test
builtin echo help pwd times
caller enable history read trap
cd eval jobs readarray true
command exec kill readonly type
如果你使用 help
命令,你將看到一個內建命令列表以及簡短描述。但是,這個列表被截斷了(以 help
命令結尾):
$ help
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or histo>
(( expression )) if COMMANDS; then COMMANDS; [ elif CO>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs ->
: kill [-s sigspec | -n signum | -sigsp>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
alias [-p] [name[=value] ... ] logout [n]
bg [job_spec ...] mapfile [-d delim] [-n count] [-O ori>
bind [-lpsvPSVX] [-m keymap] [-f filen> popd [-n] [+N | -N]
break [n] printf [-v var] format [arguments]
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] pwd [-LP]
case WORD in [PATTERN [| PATTERN]...) > read [-ers] [-a array] [-d delim] [-i>
cd [-L|[-P [-e]] [-@]] [dir] readarray [-d delim] [-n count] [-O o>
command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] or>
compgen [-abcdefgjksuv] [-o option] [-> return [n]
complete [-abcdefgjksuv] [-pr] [-DEI] > select NAME [in WORDS ... ;] do COMMA>
compopt [-o|+o option] [-DEI] [name ..> set [-abefhkmnptuvxBCHP] [-o option-n>
continue [n] shift [n]
coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
declare [-aAfFgilnrtux] [-p] [name[=va> source filename [arguments]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [jobspec ... | pid . <p>'> test [expr]
echo [-neE] [arg ...] time [-p] pipeline
enable [-a] [-dnps] [-f filename] [nam> times
eval [arg ...] trap [-lp] [[arg] signal_spec ...]
exec [-cl] [-a name] [command [argumen> true
exit [n] type [-afptP] name [name ...]
export [-fn] [name[=value] ...] or exp> typeset [-aAfFgilnrtux] [-p] name[=va>
false ulimit [-SHabcdefiklmnpqrstuvxPT] [li>
fc [-e ename] [-lnr] [first] [last] or> umask [-p] [-S] [mode]
fg [job_spec] unalias [-a] name [name ...]
for NAME [in WORDS ... ] ; do COMMANDS> unset [-f] [-v] [-n] [name ...]
for (( exp1; exp2; exp3 )); do COMMAND> until COMMANDS; do COMMANDS; done
function name { COMMANDS ; } or name (> variables - Names and meanings of som>
getopts optstring name [arg] wait [-fn] [id ...]
hash [-lr] [-p pathname] [-dt] [name .> while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
從上面的清單中可以看出,help
命令本身就是內建的。
你可以通過向 help
命令提供你感興趣的內建命令名稱來獲取關於它們的更多資訊,例如 help dirs
:
$ help dirs
dirs: dirs [-clpv] [+N] [-N]
Display directory stack.
Display the list of currently remembered directories. Directories
find their way onto the list with the `pushd' command; you can get
back up through the list with the `popd' command.
Options:
-c clear the directory stack by deleting all of the elements
-l do not print tilde-prefixed versions of directories relative
to your home directory
-p print the directory stack with one entry per line
-v print the directory stack with one entry per line prefixed
with its position in the stack
Arguments:
+N Displays the Nth entry counting from the left of the list
shown by dirs when invoked without options, starting with
zero.
-N Displays the Nth entry counting from the right of the list
shown by dirs when invoked without options, starting with
zero.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
內建命令提供了每個 shell 的大部分功能。你使用的任何 shell 都有一些內建命令,但是如何獲取這些內建命令的資訊可能因 shell 而異。例如,對於 zsh
,你可以使用 man zshbuiltins
命令獲得其內建命令的描述。
$ man zshbuiltins
ZSHBUILTINS(1) General Commands Manual ZSHBUILTINS(1)
NAME
zshbuiltins - zsh built-in commands
SHELL BUILTIN COMMANDS
Some shell builtin commands take options as described in individual en‐
tries; these are often referred to in the list below as `flags' to avoid
confusion with shell options, which may also have an effect on the behav‐
iour of builtin commands. In this introductory section, `option' always
has the meaning of an option to a command that should be familiar to most
command line users.
…
在這個冗長的手冊頁中,你將找到一個內建命令列表,其中包含有用的描述,如下摘錄中所示:
bg [ job ... ]
job ... &
Put each specified job in the background, or the current job if
none is specified.
bindkey
See the section `Zle Builtins' in zshzle(1).
break [ n ]
Exit from an enclosing for, while, until, select or repeat loop.
If an arithmetic expression n is specified, then break n levels
instead of just one.
最後
Linux 內建命令對於每個 shell 都很重要,它的操作類似特定於 shell 的命令一樣。如果你經常使用不同的 shell,並注意到你經常使用的某些命令似乎不存在或者不能按預期工作,那麼它可能是你使用的其他 shell 之一中的內建命令。
via: https://www.networkworld.com/article/3410350/getting-help-for-linux-shell-built-ins.html
作者:Sandra Henry-Stocker 選題:lujun9972 譯者:MjSeven 校對:wxy
訂閱“Linux 中國”官方小程式來檢視
相關文章
- Linux 獲取幫助Linux
- Linux檢視內建命令和非內建命令幫助的幾種方法(man、help、info)Linux
- 4.2.3 獲取SRVCTL 的幫助
- 說說Linux shell外部命令與內建命令Linux
- python-help()獲取關於物件的幫助資訊Python物件
- 萬能的Linux幫助命令有哪些?linux運維入門基礎Linux運維
- Linux設定中文幫助文件、常見目錄、幫助命令Linux
- 重新點亮linux 命令樹————幫助命令[一]Linux
- 有問題問男人,linux幫助文件-----man命令講解Linux
- Linux c程式中獲取shell指令碼輸出(如獲取system命令輸出)LinuxC程式指令碼
- shell內建命令 #yyds乾貨盤點#
- Linux shell命令列檢視判斷檔案是內部內建自帶命令還是外部命令Linux命令列
- Hadoop幫助命令一Hadoop
- Linux幫助文件Linux
- Mac/Linux 安裝中文版 man 幫助命令MacLinux
- Linux常用內建命令有哪些?Linux系統怎麼學習Linux
- 獲取Linux本機IP命令Linux
- win10系統遇到問題時獲取幫助的方法Win10
- 如何在Linux命令列下檢視幫助?學習linux運維Linux命令列運維
- Linux Shell獲取正在執行指令碼的絕對路徑Linux指令碼
- ELK相關幫助文件
- 在Linux中,bash shell 中的 hash 命令有什麼作用?Linux
- docker筆記7-docker的幫助命令Docker筆記
- Linux shell命令總結Linux
- 建議提供CHM版的幫助檔案
- Linux基礎命令---mirror獲取ftp目錄LinuxFTP
- Linux基礎命令---mget獲取ftp檔案LinuxFTP
- Linux基礎命令---get獲取ftp檔案LinuxFTP
- Node.js 執行 shell 命令 主程式獲取返回值Node.js
- man 命令顯示簡體中文幫助
- Linux獲取10個你最常用的終端命令列表Linux命令列
- 什麼是shell?Linux常用的shell有哪些?Linux
- Apple開發_Xcode內還有幫助程式碼自動排版的工具嗎?APPXCode
- Linux技術相關命令有哪些Linux
- 通過用shellcode獲取shell
- sed內建命令字元有什麼功能?linux作業系統學習字元Linux作業系統
- linux Shell 命令列-03-array Shell 陣列Linux命令列陣列
- Linux基礎命令---enable開啟shell命令Linux