16

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.

2
  • 1
    Somehow a search for the Windows equivalent brought me here; for future viewers: "where" will tell you the given file's path. Feb 5, 2014 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, 2014 at 23:40

2 Answers 2

26

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

5
  • 3
    I like type -a myself. why the brackets in [t]ype? Feb 5, 2014 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, 2014 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, 2014 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, 2014 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, 2014 at 5:09
10

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.

3
  • command -v is the equivalent of ksh whence. Feb 5, 2014 at 16:24
  • 1
    So we have where, whence, and of course also who, why, and when? Why is there no what? Feb 5, 2014 at 21:06
  • echo 'main(){printf("what\n");}' | sudo cc -x c - -o /usr/local/bin/what Feb 6, 2014 at 10:50

You must log in to answer this question.

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