![设置 bash shell 选项“checkhash”是否真的可以提高 bash 命令的性能?](https://linux22.com/image/167956/%E8%AE%BE%E7%BD%AE%20bash%20shell%20%E9%80%89%E9%A1%B9%E2%80%9Ccheckhash%E2%80%9D%E6%98%AF%E5%90%A6%E7%9C%9F%E7%9A%84%E5%8F%AF%E4%BB%A5%E6%8F%90%E9%AB%98%20bash%20%E5%91%BD%E4%BB%A4%E7%9A%84%E6%80%A7%E8%83%BD%EF%BC%9F.png)
前提:在修改的过程中.bashrc
,经过shopt 在线 bash 手册可以处理的不同 shell 选项的页面shopt
,我遇到了该选项checkhash
,根据描述:
checkhash
If this is set, Bash checks that a command found in the hash table exists
before trying to execute it.
If a hashed command no longer exists, a normal path search is performed.
问题:
- 这个选项有用吗,即它是否提高了 bash 命令的性能?
- 如果是的话,为什么是默认值设置
off
? - 如果,不,为什么这个选项首先存在,是否与旧硬件有关?
答案1
我认为该设置应该改善用户体验,而不是“性能”。
hash -r
当您移动或删除可执行文件时,它应该可以使您不必手动运行。
比较:
bash$ mkdir -p first second; PATH=$PATH:first:second
bash$ echo echo ok > first/ok; chmod 755 first/ok
bash$ ok
ok
bash$ mv first/ok second/ok
bash$ ok
bash: first/ok: No such file or directory
# Yet it's in the PATH!
bash$ hash -r
bash$ ok
ok
相对
bash$ shopt -s checkhash
bash$ mv second/ok first/ok
bash$ ok
ok
bash$ mv first/ok second/ok
bash$ ok
ok
bash$