1. 程式人生 > >Linux/Unix Bash Shell 所有內建命令

Linux/Unix Bash Shell 所有內建命令

對於bash shell本身包含的內建命令,我們如何在Linux / Apple OS X / *BSD / 類Unix作業系統上全部列出他們,並且無需查閱大量得bash手冊?

shell 內建命令就是一個命令或一個函式,從 shell 中呼叫,它直接在 shell 中執行。bash shell 直接執行該命令而無需呼叫其他程式。你可以使用help命令檢視bash內建命令的資訊。這裡有很多種型別的內建命令。

內建命令型別

1.Bourne Shell 內建命令:內建命令繼承自 Bourne Shell。

2.Bash內建命令:特定於 Bash 的內建命令表

3.修改 Shell 行為:修改 shell 屬性和可選行為的內建命令。

4.特別的內建命令:由 POSIX 特別分類的內建命令。

如何檢視bash內建命令

有以下的命令:

$ help
$ help | less
$ help | grep read

樣例輸出:

[[email protected] ~]#  help
GNU bash, version 4.1.2(2)-release (x86_64-redhat-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 history -anrw [filename] or history -ps arg [arg...]
 (( expression ))                                                                              if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
 . filename [arguments]                                                                        jobs [-lnprs] [jobspec ...] or jobs -x command [args]
 :                                                                                             kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
 [ arg... ]                                                                                    let arg [arg ...]
 [[ expression ]]                                                                              local [option] name[=value] ...
 alias [-p] [name[=value] ... ]                                                                logout [n]
 bg [job_spec ...]                                                                             mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
 bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell->  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]...) COMMANDS ;;]... esac                                    read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeou>
 cd [-L|-P] [dir]                                                                              readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array>
 command [-pVv] command [arg ...]                                                              readonly [-af] [name[=value] ...] or readonly -p
 compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] >  return [n]
 complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist]  [->  select NAME [in WORDS ... ;] do COMMANDS; done
 compopt [-o|+o option] [-DE] [name ...]                                                       set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
 continue [n]                                                                                  shift [n]
 coproc [NAME] command [redirections]                                                          shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFilrtux] [-p] [name[=value] ...]                                                 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] [name ...]                                                  times
 eval [arg ...]                                                                                trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [arguments ...]] [redirection ...]                              true
 exit [n]                                                                                      type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or export -p                                                  typeset [-aAfFilrtux] [-p] name[=value] ...
 false                                                                                         ulimit [-SHacdefilmnpqrstuvx] [limit]
 fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]                              umask [-p] [-S] [mode]
 fg [job_spec]                                                                                 unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMANDS; done                                                  unset [-f] [-v] [name ...]
 for (( exp1; exp2; exp3 )); do COMMANDS; done                                                 until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name () { COMMANDS ; }                                        variables - Names and meanings of some shell variables
 getopts optstring name [arg]                                                                  wait [id]
 hash [-lr] [-p pathname] [-dt] [name ...]                                                     while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]                                                                     { COMMANDS ; }
[
[email protected]
~]#

另外一種選擇是使用下列命令:

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
[[email protected] ~]# 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.
[[email protected] ~]# type -a cd
cd is a shell builtin
[[email protected] ~]# command -V ls
ls is aliased to `ls --color=auto'
[[email protected] ~]# 

 

 

翻譯自:

Linux / Unix Bash Shell List All Builtin Commands

https://www.cyberciti.biz/faq/linux-unix-bash-shell-list-all-builtin-commands/