【问题标题】:Bash: How to search the executables in path?Bash:如何在路径中搜索可执行文件?
【发布时间】:2012-04-19 11:45:33
【问题描述】:

我在 /usr/bin 中有一个“gtags”可执行文件,但这是一个较旧的文件。 然后我编译了一个新的 gtags 并且“make install”把它放在 /usr/local/bin 中。 然后我删除了 /usr/bin 中的那个。但是当我在 shell 中输入“gtags”时,它告诉我找不到“/usr/bin/gtags”。但是我在 /usr/local/bin 中有一个“gtags”,而 /usr/local/bin 在 PATH 中。另外,在我的 PATH 中,/usr/local/bin 在 /usr/bin 之前搜索,为什么 shell 只检查 /usr/bin 中的“gtags”?

【问题讨论】:

  • 检查可执行文件的权限。同时验证which <executable>的OP
  • @Abhijit 权限没问题,755。并且哪个 gtags 显示“/usr/local/bin/gtags”

标签: shell path


【解决方案1】:

使用 bash 内置命令hash:

[root@localhost ~]# touch /bin/test.sh
[root@localhost ~]# chmod 755 /bin/test.sh
[root@localhost ~]# test.sh
[root@localhost ~]# mv /bin/test.sh /usr/local/bin/
[root@localhost ~]# test.sh
-bash: /bin/test.sh: No such file or directory
[root@localhost ~]# hash test.sh
[root@localhost ~]# test.sh

发生这种情况的原因是 bash “缓存”,其中二进制文件位于 $PATH 中,因此它不必在每次运行时都找到它。这是表演的事情。如果你运行不带参数的“hash”,它会报告它运行了哪些命令,运行了多少次,以及它从哪里运行。

【讨论】:

  • 谢谢!这真的很有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
相关资源
最近更新 更多