Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
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 Jul 22 '11 at 10:56
    
Brendan: See my above comment (under laebshade's answer). Also, what gives type -a which and echo $PATH? –  Philomath Jul 22 '11 at 11:30
    
I voted this the answer since I hadn't heard of the type command before. Thanks. –  Brendan Jul 22 '11 at 12:11
add comment

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').

share|improve this answer
1  
Does MacOS X default shell has something akin to type in bash? –  Vitor Jul 22 '11 at 0:12
    
Mac OS X shell, AFAIK, is bash. –  Denilson Sá Jul 22 '11 at 0:42
    
It uses tcsh or bash, depending on the version. See: aplawrence.com/MacOSX/macosxshell.html –  laebshade Jul 22 '11 at 4:27
    
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 Jul 22 '11 at 10:53
    
@Brendan: can you do hash -d python, than type -a python? what's the output? –  Philomath Jul 22 '11 at 11:16
show 1 more comment

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.

share|improve this answer
    
I believe that MacOS X uses the FreeBSD userland which doesn't say anything about bash (unlike GNU): freebsd.org/cgi/… –  Vitor Jul 22 '11 at 0:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.