Bash 记住了被移动/删除的可执行文件的错误路径

spi*_*ace 38 bash path which

当我做

which pip3
Run Code Online (Sandbox Code Playgroud)

我得到

/usr/local/bin/pip3
Run Code Online (Sandbox Code Playgroud)

但是当我尝试执行时pip3,出现如下错误:

bash: /usr/bin/pip3: No such file or directory
Run Code Online (Sandbox Code Playgroud)

这是因为我最近删除了该文件。现在which命令指向的另一个版本pip3是位于/usr/local/bin但外壳还记得在错误的道路。我如何让它忘记那条路?

which手册说

which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in
       a strictly POSIX-conformant shell.  It does this by searching the PATH for executable files matching the names of the arguments. It does not follow
       symbolic links.
Run Code Online (Sandbox Code Playgroud)

双方/usr/local/bin/usr/bin都在我的PATH变量,而/usr/local/bin/pip3不是一个符号链接,它是一个可执行文件。那么为什么不执行呢?

Eri*_*ouf 49

当您在bash其中运行命令时,它会记住该可执行文件的位置,因此不必PATH每次都再次搜索。所以如果你运行可执行文件,然后更改位置,bash仍然会尝试使用旧位置。您应该能够确认这一点,hash -t pip3这将显示旧位置。

如果您运行hash -d pip3它,它会告诉 bash 忘记旧位置,下次尝试时应该找到新位置。

  • 或者`hash -r` 来清除整个表格。 (6认同)
  • @spiderface `type hash` 会告诉你它是一个内置的 shell,所以它没有自己的手册页。相反,使用 `help hash` 或在 bash 的手册页中查找 `hash`。 (3认同)
  • 既然 bash 在下面调用 hash -t pip3 ,为什么它在发现文件不存在时不自动调用 hash -d pip3 呢?或者至少发出警告和建议来运行“hash -d pip3”? (3认同)