1. 程式人生 > >更新Bash路徑的緩存

更新Bash路徑的緩存

isp clas div use val 問題 ons hit edi

---恢復內容開始---

1、登陸一個新的vps時候,發現git的版本是1.8的,太久了,於是就源碼安裝了新的版本2.4。

2、老版本在/usr/bin/git,新版本安裝的/usr/local/bin/git

3、問題來了,安裝完新的後,卸載了舊的版本,在運行git --version時,卻提示如下錯誤:

-bash: /usr/bin/git: No such file or directory

4、我去,bash只搜索舊的路徑,查看了一下PATH變量:

[[email protected] bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

沒有問題,而且/usr/local/bin還在前面,但系統卻不搜索,我暈,這是啥問題?

5、經過一番折騰,終於找到答案,原來Bash路徑也是有緩存地,只要運行過的命令,bash就會保存到一張hash表裏,下次直接從hash表裏找地址,就不按個路徑找了。

6、運行:help hash查看一下:

[[email protected] bin]# help hash
hash: hash [-lr] [-p pathname] [-dt] [name ...]
    Remember or display program locations.

    Determine and remember the full pathname of each command NAME.  If
    no arguments are given, information about remembered commands is displayed.

    Options:
      
-d forget the remembered location of each NAME -l display in a format that may be reused as input -p pathname use PATHNAME is the full pathname of NAME -r forget all remembered locations -t print the remembered location of each NAME, preceding each location with the corresponding NAME
if multiple NAMEs are given Arguments: NAME Each NAME is searched for in $PATH and added to the list of remembered commands. Exit Status: Returns success unless NAME is not found or an invalid option is given.

7、在運行hash命令,查看hash表裏都有啥?

[[email protected] bin]# hash
hits    command
   5    /usr/bin/git
   1    /usr/sbin/ifconfig
   1    /usr/bin/rm
   7    /usr/bin/vim
   1    /usr/bin/wget
   1    /usr/bin/mv
  19    /usr/bin/yum
   1    /usr/bin/ln
   1    /usr/bin/man
   5    /usr/bin/make
   1    /usr/bin/tar
  15    /usr/bin/ls

果然,第一個就是git,而且地址還是老地址。

8、運行: hash -d git 刪除掉git的緩存。

9、再次執行git --version 哈哈,正確顯示了新安裝的版本。

---恢復內容結束---

更新Bash路徑的緩存