什么决定了系统搜索文件的路径?

rip*_*234 5 bash executable

我试图让npm工作。在这个过程中,我似乎安装了它的两个版本:一个安装在 ~/bin 中的损坏的一个,另一个我刚刚编译并运行make install将它放入/usr/local/bin/npm.

所以,我将整个 ~/bin 文件夹移到了 ~/old/bin ......但是当我npm在 ~/bin 中运行系统搜索时:

$ which npm
/usr/local/bin/npm
$ alias npm
-bash: alias: npm: not found
$ npm
-bash: /home/ubuntu/bin/npm: No such file or directory
$ echo $PATH
/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
$
Run Code Online (Sandbox Code Playgroud)

是什么导致 Ubuntu 在 中寻找 npm ~/bin/npm?我正在使用 Ubuntu 11.10。我不知道问题是否特定于 ubuntu,如果需要,可以将其移至 askubuntu。

我确实在 PATH 中看到了 ~/bin,但据我所知,这仅意味着如果 npm 出现在 ~/bin 中,那么它就会使用它……但是为什么 bash 坚持在那里专门寻找它?为什么它没有 find /usr/local/bin/npm,即使which命令确实找到了它?

Chr*_*own 15

可执行文件先前已知的位置可能已被 shell 散列。重置外壳的缓存hash -r应该可以解决这个问题。

如果不想重置整个缓存,可以删除单个条目以npm使用hash -d npm.

  • @ripper234 `help hash` 也包含一些很好的信息。 (5认同)
  • @ripper234 - `hash` 是一个 shell 内置命令。bash 联机帮助页包含更多详细信息。 (2认同)