bash命令执行
命令分类
首先,我们先来聊聊命令的分类吧!在shell中可执行的命令一共有两类,分别为内部命令和外部命令。
内部命令
由shell程序自带的命令,最常见的内部命令有cd、pwd等。
那么我们怎么判断命令是内部还是外部的呢?
type命令是用来显示指定命令的类型,判断给出的指令是内部命令还是外部命令。
示例:
[root@centos6 ~]# type type
type is a shell builtin #type命令是内部命令
[root@centos6 ~]# type ip
ip is /sbin/ip #ip命令是外部命令
如何查看内部命令呢?
help 显示内部命令列表
示例:
[root@centos6 ~]# 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 [fi>
(( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS; then>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x command [a>
: kill [-s sigspec | -n signum | -sigspec] pid | jo>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
。。。
启用或关闭内部命令:
enable 显示内部命令列表
enable COMMAND 启用内部命令(COMMAND指的是内部命令)
ebable -n COMMAND 禁用内部命令(COMMAND指的是内部命令)
enable -n 查看所有禁用的内部命令
示例:
[root@centos6 ~]# enable #显示内部命令列表
enable .
enable :
enable [
enable alias
enable bg
enable bind
。。。
[root@centos6 ~]# enable -n cd #禁用内部命令cd
[root@centos6 ~]# enable -n #查看禁用的内部命令
enable -n cd
[root@centos6 ~]# enable cd #启用内部命令cd
[root@centos6 ~]# enable -n #查看禁用的内部命令
命令别名alias
命令别名是一个很好用的命令,我们可以将一些命令较长的命令进行简化。
alias用法:
alias 显示当前shell进程所有可用的命令别名
alias NAME=‘VALUE’ 定义别名NAME,相当于执行命令VALUE
PS:在shell中定义的别名,仅对当前shell生效。
如果想永久生效,需要写到配置文件中。
仅对当前用户:~/.bashrc
对所有用户生效:/etc/bashrc
外部命令
外部命令本身是一个可执行程序文件,命令名即为程序名,是可以在磁盘中找到的。常见的外部命令有ls、ifconfig等。
如何查看外部命令?
通过shell内置的环境PATH指定的路径来查找。
which 用来查找并显示给定命令的路径
whereis 用来定位指定命令的的路径及man手册页等相关文件的路径。
示例:
[root@centos6 ~]# echo $PATH #显示PATH环境变量
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos6 ~]# which ifconfig #查找ifconfig命令的路径
/sbin/ifconfig
[root@centos6 ~]# whereis ifconfig #查找ifconfig命令的路径
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@centos6 ~]# ls /sbin/ifconfig #查看命令
/sbin/ifconfig
Hash缓存表
提到了外部命令,Hash缓存表是一定要给大家说一下的~~
hash的作用是shell每次执行环境变量PATH中的一个命令时,hash都会记住它。以便于下次查找外部命令时就不必搜索完整路径了,速度更快一些。
hash常见用法:
hash 显示hash缓存
hash -l 显示hash缓存及命令的完整路径等信息
hash -p path name 将命令全路径path起别名为name
hash -t name 打印缓存中name的路径
hash -d name 清除name缓存
hash -r 清除缓存
示例:
[root@centos6 ~]# hash #查看hash缓存
hits command
5 /sbin/ifconfig
2 /usr/bin/whereis
4 /usr/bin/man
7 /bin/ls
[root@centos6 ~]# hash -l #查看hash缓存及路径
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /usr/bin/whereis whereis
builtin hash -p /usr/bin/man man
builtin hash -p /bin/ls ls
[root@centos6 ~]# hash -p /usr/bin/whereis wh #将hash表中的whereis命令定义别名为wh
[root@centos6 ~]# wh ls #执行刚刚定义的别名
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@centos6 ~]# hash -t ls #查看ls命令的路径
/bin/ls
[root@centos6 ~]# hash -d ls #清除hash表中的ls命令
[root@centos6 ~]# hash #查看hash表,验证ls是否别删除
hits command
5 /sbin/ifconfig
2 /usr/bin/whereis
4 /usr/bin/man
2 /usr/bin/whereis
[root@centos6 ~]# hash -r #清除全部缓存
[root@centos6 ~]# hash #查看hash表,验证是否被全部清除
hash: hash table empty
命令执行顺序
先给大家来一张图,方便理解一点。因为是第一次作图,做的不好多多见谅,哈哈~~
那我们接下来验证一下:
hash表比外部命令优先级高
[root@centos6 ~]# type hostname #确定hostname为外部命令
hostname is /bin/hostname
[root@centos6 ~]# hostname #执行外部命令
centos6.magedu.com
[root@centos6 ~]# hash #查看hash表,hostname已被缓存
hits command
1 /bin/hostname
[root@centos6 ~]# mv /bin/hostname /usr/local/bin/ #移动hostname到/usr/local/bin/目录下
[root@centos6 ~]# hostname #执行hostname命令发现找不到命令
-bash: /bin/hostname: No such file or directory
[root@centos6 ~]# hash -r #清空hash表
[root@centos6 ~]# hostname #执行命令hostname,命令存在
centos6.magedu.com
[root@centos6 ~]# hash #查看hash表,发现路径已改变。说明hash缓存优先级比外部命令高。
hits command
1 /usr/local/bin/hostname
内部命令比hash表和外部优先级高
[root@centos6 ~]# hash #查看hash表,没有内容
hash: hash table empty
[root@centos6 ~]# type cd #查看cd命令类型,是内部命令
cd is a shell builtin
[root@centos6 ~]# whereis cd #cd同时也是外部命令
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
[root@centos6 ~]# cd #执行cd命令
[root@centos6 ~]# hash #查看hash表,表中没有。说明内部命令比hash表和外部命令优先级高。
hits command
1 /usr/bin/whereis
别名比内部命令优先级高
[root@centos6 ~]# hash #查看hash表,没有内容
hash: hash table empty
[root@centos6 ~]# type cd #确认cd为内部命令
cd is a shell builtin
[root@centos6 ~]# type hostname #确认hostname为外部命令
hostname is /usr/local/bin/hostname
[root@centos6 ~]# alias cd='hostname' #定义别名cd=hostname
[root@centos6 ~]# cd #执行cd命令,显示的而是hostname执行后的结果。说明了别名比内部命令优先级高。
centos6.magedu.com
网友评论