Linux【8】-软件管理-9-软件安装1-缓存表hash

系统初始hash表为空,当外部命令执行时,默认会从PATH路径下寻找该命令,找到后会将这条命令的路径记录到hash表中,当再次使用该命令时,shell解释器首先会查看hash表,存在将执行之,如果不存在,将会去PATH路径下寻找,利用hash缓存表可大大提高命令的调用速率

一、hash 显示hash缓存

[root@CentOS7 ~]$hash
hits	command
   3	/usr/bin/apropos
   3	/usr/bin/whatis
   1	/usr/bin/systemctl
  22	/usr/bin/man

二、hash –l 显示hash缓存,可作为输入使用

-l参数带有命令的全路径

[root@CentOS7 ~]$hash -l
builtin hash -p /usr/bin/apropos apropos
builtin hash -p /usr/bin/whatis whatis
builtin hash -p /usr/bin/systemctl systemctl
builtin hash -p /usr/bin/man man

三、hash –p path name将命令全路径path起别名为name

就是定义环境变量

[root@CentOS7 ~]$hash
hits	command
   0	/usr/bin/free
   2	/usr/bin/lsblk
   2	/usr/bin/uname
   1	/usr/bin/man
   1	/usr/bin/tr
   1	/usr/bin/ls
   1	/usr/bin/tree
   2	/usr/bin/free
[root@CentOS7 ~]$hash -p /usr/bin/free Myfree

`free=Myfree`

[root@CentOS7 ~]$Myfree
              total        used        free      shared  buff/cache   available
Mem:         995896      187620      426380        3532      381896      596012
Swap:       2097148       26276     2070872
[root@CentOS7 ~]$free
              total        used        free      shared  buff/cache   available
Mem:         995896      187404      426596        3532      381896      596228
Swap:       2097148       26276     2070872

四、hash –t name 打印缓存中name的路径

[root@CentOS7 ~]$hash
hits	command
   1	/usr/bin/lsblk
   2	/usr/bin/uname
   1	/usr/bin/man
   1	/usr/bin/tr
   1	/usr/bin/ls
   1	/usr/bin/tree
   2	/usr/bin/free

[root@CentOS7 ~]$hash -t lsblk
/usr/bin/lsblk

五、hash –d name 清除name缓存

[root@pp]# hash
hits    command
   1    /usr/bin/vim
   7    /usr/bin/ls


[root@pp]# hash -d vim


[root@pp]# hash
hits    command
   7    /usr/bin/ls

六、hash –r 清除缓存

[root@CentOS7 ~]$hash 
hits	command
   3	/usr/bin/apropos
   3	/usr/bin/whatis
   1	/usr/bin/systemctl
  22	/usr/bin/man
  
[root@CentOS7 ~]$hash -r
[root@CentOS7 ~]$hash
hash: hash table empty

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn