shell的内建命令和外部命令

shell的内建命令和外部命令

Shell执行的命令可以分为内建命令(built-in)和外部命令(external),前者是构建在shell内部;后者是一个独立的文件(可以是二进制文件,也可以是一个脚本)。

内建命令的执行比较简单,由当前shell本身来执行,例如echo, eval, exec, export, readonly, read, shift, wait和点(.)等;

外部命令的执行比较复杂,shell进程会fork一个子进程,父进程随后挂起,然后在子进程中exec加载外部文件,子进程返回后,父进程才继续执行。

从上面的区别可以看出,内建命令的性能要优于外部命令,因为它没有新建进程的开销。

使用type命令可以区别一个命令是内建命令,还是外部命令:

[root@localhost tmp]# type cd
cd is a shell builtin
[root@localhost tmp]# type awk
awk is /bin/awk

注意,Shell会沿着路径$PATH来寻找外部命令,环境变量PATH是一个以冒号分隔的目录列表,

[root@localhost tmp]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

最后,使用help命令可以查看内建命令列表:

[root@localhost tmp]# help
GNU bash, version 4.1.()-release (x86_64-unknown-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] o>
(( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x command [args]
: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...>
[ 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>
bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u nam> 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] [->
cd [-L|-P] [dir] readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [>
command [-pVv] command [arg ...] readonly [-af] [name[=value] ...] or readonly -p
compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpa> return [n]
complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action> 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] [com> 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 ; }
上一篇:css案例学习之并集选择器


下一篇:怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).