linux hash有效时间,Linux的命令缓存机制(命令:hash)

简介

hash命令是bash的内置命令。

我们知道在bash中执行外部命令,会根据环境变量PATH来逐一搜索命令的路径。

hash就是用于记住命令的路径,并且在下次执行命令的时候直接通过hash获取而不再通过PATH一步步寻找,加快了寻找命令的速度。

hash就像是命令的缓存。

语法

hash [-lr] [-p pathname] [-dt] [name ...]

语法中的name就是每个命令的名称,例如:ls、grep、cat等。

选项与示例

当不使用任何选项和参数的时候,hash命令可以输出当前所记住的命令以及其缓存命中次数。

一开始没有执行外部命令的时候,hash是空的。

[root@C7 ~]# hash

hash: hash table empty

执行了几个命令之后。如果某些命令有重复执行,那么命中数会大于1。

[root@C7 ~]# hash

hits command1 /usr/bin/grep

1 /usr/bin/tail

1 /usr/bin/head

2 /usr/bin/ls

-d:删除已记住的命令的路径。

[root@C7 ~]# hash

hits command1 /usr/bin/grep

1 /usr/bin/ls[root@C7~]# hash -d grep[root@C7~]# hash

hits command1 /usr/bin/ls

-l:也是输出当前缓存中的内容,只不过输出的格式可用于输入或者再执行。

[root@C7 ~]# hash -l

builtin hash-p /usr/bin/ls ls

-p pathname:为某个命令手动设置路径。一般用于当命令的位置发生改变的情况。若出现该情况也可以使用-d删除缓存再重新执行一遍。

注意:如果指定了一个错误的路径的话,那么命令就无法使用了,如下所示。

[root@C7 ~]# hash -p /tmp/grep grep[root@C7~]# hash

hits command0 /tmp/grep

0 /usr/bin/tail[root@C7~]# grep 'root' /etc/passwd

-bash: /tmp/grep: No such file or directory

-r:清空缓存列表。

[root@C7 ~]# hash

hits command1 /usr/bin/sort

1 /usr/bin/cat

1 /usr/bin/ls[root@C7~]# hash -r

[root@C7~]# hash

hash: hash table empty

-t:显示在缓存中,命令的对应路径。命令不在缓存中则会报错。

[root@C7 ~]# hash

hits command1 /usr/bin/head

1 /usr/bin/du

1 /usr/bin/cat

1 /usr/bin/less

1 /usr/bin/ls[root@C7~]# hash -t ls

/usr/bin/ls[root@C7~]# hash -t less

/usr/bin/less[root@C7~]# hash -t ls less grep

ls /usr/bin/ls

less /usr/bin/less

-bash: hash: grep: not found

hash name:如果仅带参数不带选项的话,hash可用于清空具体命令的缓存命中次数。这个功能是不小心发现的,在“help hash”中并无说明。

[root@C7 ~]# hash

hits command1 /usr/bin/head

5 /usr/bin/wc[root@C7~]# hash wc[root@C7~]# hash

hits command1 /usr/bin/head

0 /usr/bin/wc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值