type命令及Linux命令类型

来源:互联网 发布:建筑建模软件 编辑:程序博客网 时间:2024/04/27 21:55

type后跟一个指令,会打印出指令的类型,如不带选项,展示当该指令作为一个命令时是如何解读的。

参数-a,type会打印指令的所有位置。
参数-t,type会打印alias,keyword,function,builtin,file的之一。alias:别名。 keyword:Shell保留字。 function: Shell函数。 builtin: Shell内建命令。 file:磁盘文件,外部命令。

当我们键入某个命令时, shell会按照alias->keyword->function->builtin->$PATH的顺序进行搜索, 本着”先到先得”的原则, 就是说如果有如名为mycmd的命令同时存在于alias和function中的话, 那么肯定会使用alias的mycmd命令。但是hash比它们的优先级都高。

内建命令:
shell内建命令是指bash(或其它版本)工具集中的命令。一般都会有一个与之同名的系统命令,比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他们行为大体相仿。内建命令比系统论命令有比较高的执行效率,外部命令执行时往往需要fork一个子进程,而内建命令一般不用。

hash:
linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样。第一次执行命令shell解释器默认的会从PATH路径下寻找该命令的路径,当你第二次使用该命令时,shell解释器首先会查看hash表,没有该命令才会去PATH路径下寻找。输入hash可以查看hash表的内容,hash –p a b添加一项a改名为b,执行b时实际会执行a命令。

bash所有的内建指令:
builtin

借用别人的例子:
[root@linuxidc ~]# type -a cd
cd is a shell builtin
[root@linuxidc ~]# type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
[root@linuxidc ~]# type -a time
time is a shell keyword
time is /usr/bin/time
[root@linuxidc ~]# type -a date
date is /bin/date
[root@linuxidc ~]# type -a which
which is aliased to `alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde’
which is /usr/bin/which
[root@linuxidc ~]# type -a whereis
whereis is /usr/bin/whereis
[root@linuxidc ~]# type -a whatis
whatis is /usr/bin/whatis
[root@linuxidc ~]# type -a function
function is a shell keyword
[root@linuxidc ~]# type -a ls
ls is aliased to `ls –color=tty’
ls is /bin/ls
[root@linuxidc ~]# type -a ll
ll is aliased to `ls -l –color=tty’
[root@linuxidc ~]# type -a echo
echo is a shell builtin
echo is /bin/echo
[root@linuxidc ~]# type -a bulitin
-bash: type: bulitin: not found
[root@linuxidc ~]# type -a builtin
builtin is a shell builtin
[root@linuxidc ~]# type -a keyword
-bash: type: keyword: not found
[root@linuxidc ~]# type -a command
command is a shell builtin
[root@linuxidc ~]# type -a alias
alias is a shell builtin
[root@linuxidc ~]# type -a grep
grep is /bin/grep

1 0
原创粉丝点击