Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've been testing a minimal Fedora install. To check the path for interpreters like python or node, I normally use which.

I notice which isn't installed by default. I could add the package, but I wonder if there's a shell builtin that can be used to perform this common task.

I'm using bash 4.2.

share|improve this question
1  
Somehow a search for the Windows equivalent brought me here; for future viewers: "where" will tell you the given file's path. –  Rubber Duck Feb 5 at 22:11
1  
I encourage anyone that comes across this Q to also read this U&L Q&A. It covers everything you ever wanted to know about which. Why not use “which”? What to use then? –  slm Feb 5 at 23:40

2 Answers 2

up vote 27 down vote accepted

You can use type, which is a Bash builtin:

$ type -P which
which is /usr/bin/which

For documentation, see help [t]ype, which refers to the type section in the bash man page.

(help type prints the help pages for two builtins which start with the string "type", one of which is obsolete and completely unrelated to this.)

share|improve this answer
3  
I like type -a myself. why the brackets in [t]ype? –  glenn jackman Feb 5 at 12:56
    
type -a will print all things that the argument points to, and not just what would be executed when typing it. –  l0b0 Feb 5 at 20:19
2  
I encourage anyone that comes across this Q to also read this U&L Q&A. It covers everything you ever wanted to know about which. Why not use “which”? What to use then? –  slm Feb 5 at 23:41
    
The first thing in the list of type -a is always what it would execute. I do the same as Glenn and always use type -a too. –  slm Feb 5 at 23:42
1  
type -p doesn't show path if command is an alias. type -P always shows command path properly. i think. we should update accepted answer as type -P @l0b0 –  dirigeant Feb 6 at 5:09

You can use type or command -v. The output of type is human readable; the output of command -v can be executed by Bash.

Note that they are actually a little different. type and command look up the hashed value of the command. That is to say, if you type cmd, type cmd or command -v cmd will tell you exactly what will be run. They also work on aliases, Bash functions, and Bash builtins (although type -p will ignore these and only return true files).

which just does a search on the PATH. This is different because:

  • If there is an alias, function, or builtin with the same name, it will be called instead.
  • If a command was added earlier in the PATH since it was last hashed, it will be found by which, but executing that command will use the hashed value (you can force update the hash in Bash with hash -r).

Usually people really want type, not which, at least for interactive use, as they use it to find out "where is this command coming from when I run it?" You should only use which if you really want to do a PATH lookup.

share|improve this answer
    
command -v is the equivalent of ksh whence. –  Henk Langeveld Feb 5 at 16:24
1  
So we have where, whence, and of course also who, why, and when? Why is there no what? –  Hagen von Eitzen Feb 5 at 21:06
    
echo 'main(){printf("what\n");}' | sudo cc -x c - -o /usr/local/bin/what –  Ivan Vučica Feb 6 at 10:50

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.