作者 | Vivek Gite
譯者 | KarenMrzhang
內建命令包含在 bash shell 本身裡面。我該如何在 Linux / Apple OS X / *BSD / Unix 類作業系統列出所有的內建 bash 命令,而不用去讀大篇的 bash 操作說明頁?
shell 內建命令就是一個命令或一個函式,從 shell 中呼叫,它直接在 shell 中執行。 bash shell 直接執行該命令而無需呼叫其他程式。你可以使用 help
命令檢視 Bash 內建命令的資訊。以下是幾種不同型別的內建命令。
內建命令的型別
如何檢視所有 bash 內建命令
有以下的命令:
$ help
$ help | less
$ help | grep read
樣例輸出:
GNU bash, version 4.1.5(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 hist>
(( expression )) if COMMANDS; then COMMANDS; [ elif C>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs >
: kill [-s sigspec | -n signum | -sigs>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
alias [-p] [name[=value] ... ] logout [n]
bg [job_spec ...] mapfile [-n count] [-O origin] [-s c>
bind [-lpvsPVS] [-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] [->
cd [-L|-P] [dir] readarray [-n count] [-O origin] [-s>
command [-pVv] command [arg ...] readonly [-af] [name[=value] ...] or>
compgen [-abcdefgjksuv] [-o option] > return [n]
complete [-abcdefgjksuv] [-pr] [-DE] > select NAME [in WORDS ... ;] do COMM>
compopt [-o|+o option] [-DE] [name ..> set [--abefhkmnptuvxBCHP] [-o option>
continue [n] shift [n]
coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
declare [-aAfFilrtux] [-p] [name[=val> source filename [arguments]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [jobspec ...] test [expr]
echo [-neE] [arg ...] time [-p] pipeline
enable [-a] [-dnps] [-f filename] [na> times
eval [arg ...] trap [-lp] [[arg] signal_spec ...]
exec [-cl] [-a name] [command [argume> true
exit [n] type [-afptP] name [name ...]
export [-fn] [name[=value] ...] or ex> typeset [-aAfFilrtux] [-p] name[=val>
false ulimit [-SHacdefilmnpqrstuvx] [limit>
fc [-e ename] [-lnr] [first] [last] o> umask [-p] [-S] [mode]
fg [job_spec] unalias [-a] name [name ...]
for NAME [in WORDS ... ] ; do COMMAND> unset [-f] [-v] [name ...]
for (( exp1; exp2; exp3 )); do COMMAN> until COMMANDS; do COMMANDS; done
function name { COMMANDS ; } or name > variables - Names and meanings of so>
getopts optstring name [arg] wait [id]
hash [-lr] [-p pathname] [-dt] [name > while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
另外一種選擇是使用下列命令:
compgen -b
compgen -b | more
檢視 Bash 的內建命令資訊
執行以下得到詳細資訊:
help command
help read
要僅得到所有帶簡短描述的內建命令的串列,執行如下:
$ help -d
查詢內建命令的語法和其他選項
使用下列語法去找出更多的相關內建命令:
help name
help cd
help fg
help for
help read
help :
樣例輸出:
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds
找出一個命令是內部的(內建)還是外部的
使用 type
命令或 command
命令:
type -a command-name-here
type -a cd
type -a uname
type -a :
type -a ls
或者:
type -a cd uname : ls uname
樣例輸出:
cd is a shell builtin
uname is /bin/uname
: is a shell builtin
ls is aliased to `ls --color=auto'
ls is /bin/ls
l is a function
l ()
{
ls --color=auto
}
或者:
command -V ls
command -V cd
command -V foo
View list bash built-ins command info on Linux or Unix
關於作者
作者是 nixCraft 網站的發起人和經驗豐富的系統管理員,以及 Linux 作業系統/Unix shell 指令碼程式設計培訓師。他與全球客戶以及包括 IT、教育、國防和空間研究以及非營利部門在內的各個行業合作。可以在 Twitter[1]、Facebook[2]、 Google+[3] 上關註他。
via: https://www.cyberciti.biz/faq/linux-unix-bash-shell-list-all-builtin-commands/
作者:Vivek Gite[5] 譯者:KarenMrzhang 校對:wxy
本文由 LCTT 原創編譯,Linux中國 榮譽推出