5

I run which and get the following,

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)

My point is, which does not tell me the primary executable, i.e. the one that will be executed in preference. How do I find this out?

I am running OSX 10.6 on a Macbook although the question is general to UNIX-likes.

Update: I have been removing lots of redundant versions of Python on my system (I had at least half a dozen) and removing various crufty PATH declarations in a bunch of initialisation files. In the process, somehow, a fresh shell now shows the expected output (i.e. which shows /opt/local/bin/python and that is what is executed). In any case, thanks for the help!

3 Answers 3

9

The one that gets output when you run which without -a is the one which will get executed. (and the second one with -a is preferred over the third one).

This doesn't take into account the shell's builtins, aliases, and functions which will run (from within the shell) before any other executable.

Therefore, it's better to use type instead.

3
  • That's my point. The which command with no extra arguments, listed /opt/local/bin/python, however when I executed python /usr/local/bin/python is executed. There are no aliases according to alias. type does get it right though (/usr/local/bin) but why does it execute this file and not the other which comes before it in the PATH?
    – Brendan
    Commented Jul 22, 2011 at 10:56
  • Brendan: See my above comment (under laebshade's answer). Also, what gives type -a which and echo $PATH?
    – Philomath
    Commented Jul 22, 2011 at 11:30
  • 1
    I voted this the answer since I hadn't heard of the type command before. Thanks.
    – Brendan
    Commented Jul 22, 2011 at 12:11
5

The one that shows with only which python is the one your console will use, unless you have python aliased to something else (alias python='/usr/bin/python').

6
  • 2
    Does MacOS X default shell has something akin to type in bash?
    – Vitor Py
    Commented Jul 22, 2011 at 0:12
  • Mac OS X shell, AFAIK, is bash. Commented Jul 22, 2011 at 0:42
  • It uses tcsh or bash, depending on the version. See: aplawrence.com/MacOSX/macosxshell.html
    – laebshade
    Commented Jul 22, 2011 at 4:27
  • 1
    The shell is bash, alias outputs nothing. Interestingly type python outputs python is hashed (/usr/bin/python) i.e. prints the path to the python that is executed. Normally though I'd use zsh when I can be bothered to configure it. However zsh does not seem to have the type builtin ...
    – Brendan
    Commented Jul 22, 2011 at 10:53
  • @Brendan: can you do hash -d python, than type -a python? what's the output?
    – Philomath
    Commented Jul 22, 2011 at 11:16
1

According to the which man page, which searches components of the PATH variable using the same algorithm as bash, that is each component of PATH is searched in turn. So, as you observed, which (without the -a) will just show you the first match found in PATH.

Your example is somewhat peculiar in that which returns what would not be the "primary executable", since the first match is a symlink. There is no reference to symlinks in which's manpage, so I'm assuming it can't be made to follow them and reveal their target.

1
  • I believe that MacOS X uses the FreeBSD userland which doesn't say anything about bash (unlike GNU): freebsd.org/cgi/…
    – Vitor Py
    Commented Jul 22, 2011 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .