如何确定将运行我路径上的哪个可执行文件?

Bre*_*dan 5 path utilities which

我运行which并得到以下信息,

brendan$ which python
  /opt/local/bin/python
brendan$ which -a python
  /opt/local/bin/python
  /usr/bin/python
brendan$ ls -l /opt/local/bin/python
  lrwxr-xr-x  1 root  admin  24 22 Jul 00:45 /opt/local/bin/python -> /opt/local/bin/python2.4
brendan$ python
  Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
  ...
  (this is the python version in /usr/local/bin)
Run Code Online (Sandbox Code Playgroud)

我的观点是,which没有告诉我主要的可执行文件,即优先执行的那个。我怎么知道这个?

我在 Macbook 上运行 OSX 10.6,尽管这个问题对 UNIX 类用户很普遍。

更新:我一直在我的系统上删除许多冗余版本的 Python(我至少有六个)并删除PATH了一堆初始化文件中的各种粗俗声明。在这个过程中,不知何故,一个新的 shell 现在显示了预期的输出(即which显示/opt/local/bin/python,这就是执行的内容)。无论如何,感谢您的帮助!

Phi*_*ath 9

which没有运行时获得输出的那个-a是将被执行的那个。(第二个 with-a优于第三个)。

这没有考虑 shell 的内置函数、别名和将在任何其他可执行文件之前运行(从 shell 内)的函数。

因此,最好改为使用type


lae*_*ade 5

只显示的which python那个是您的控制台将使用的那个,除非您将 python 别名为其他东西 ( alias python='/usr/bin/python')。

  • MacOS X 默认 shell 是否类似于 bash 中的“type”? (2认同)