Unix which




















To use it at the Unix prompt, enter:. Replace command with the name of the executable for which you are looking. For example, if you are looking for the location of the lpr command, you would enter:. In this example, the query asked about the lpr command, which spools jobs to printers. The operating system returned two answers, and thus two paths. The first path is the location of the lpr executable, and the second path is the location of the lpr manual page.

To find the path the operating system uses to execute a command when you enter it on the command line, use the which command instead, for example:. This is document acec in the Knowledge Base. Last modified on Skip to: content search login. The which command is a broken heritage from the C-Shell and is better left alone in Bourne-like shells. There's a distinction between looking for that information as part of a script or interactively at the shell prompt.

At the shell prompt, the typical use case is: this command behaves weirdly, am I using the right one? What exactly happened when I typed mycmd? Can I look further at what it is? In that case, you want to know what your shell does when you invoke the command without actually invoking the command.

In shell scripts, it tends to be quite different. In a shell script there's no reason why you'd want to know where or what a command is if all you want to do is run it. Generally, what you want to know is the path of the executable, so you can get more information out of it like the path to another file relative to that, or read information from the content of the executable file at that path.

Interactively, you may want to know about all the my-cmd commands available on the system, in scripts, rarely so. The early Unix shells until the late 70s had no functions or aliases. While the Bourne shell was first released in Unix V7 earlier in , function support was only added much later in SVR2 , and anyway, it never had some rc file the. Here you go: which came first for the most popular shell at the time and csh was still popular until the mids , which is the main reason why it got documented in books and is still widely used.

Note that, even for a csh user, that which csh script does not necessarily give you the right information. A similar functionality was not added to the Bourne shell until in SVR2 with the type builtin command.

The fact that it is builtin as opposed to an external script means that it can give you the right information to some extent as it has access to the internals of the shell.

The initial type command suffered from a similar issue as the which script in that it didn't return a failure exit status if the command was not found. Unix Version 8's not released in the wild Bourne shell had its type builtin renamed to whatis and extended to also report about parameters and print function definitions. It also fixed type issue of not returning failure when failing to find a name.

The Korn shell a subset of which the POSIX sh definition is based on , developed in the mids but not widely available before , added many of the csh features line editor, aliases All of the Almquist shell ash , to be replacement of the Bourne shell in BSDs , the public domain implementation of ksh pdksh , bash sponsored by the FSF , zsh came out in-between and Ash, though meant to be a replacement for the Bourne shell, didn't have a type builtin until much later in NetBSD 1.

There are dozens of implementations of a which command on various Unices with different syntax and behaviour. On Linux beside the builtin ones in tcsh and zsh we find several implementations. There is a GNU which which is probably the most extravagant one. It tries to extend what the which csh script did to other shells: you can tell it what your aliases and functions are so that it can give you a better answer and I believe some Linux distributions set some global aliases around that for bash to do that.

So again, that answers a different need. UNIX specifies the type command no option. That's all where , which , whence are not specified in any standard. Up to some version, type and command -v were optional in the Linux Standard Base specification which explains why for instance some old versions of posh though based on pdksh which had both didn't have either. If you have an alias or function defined for it, it may or may not tell you about it, or tell you the wrong thing.

If you want to know about all the commands by a given name, there's nothing portable. You'd use where in tcsh or zsh , type -a in bash or zsh , whence -a in ksh93 and in other shells, you can use type in combination with which -a which may work. In csh and tcsh , you don't have much choice. In tcsh , that's fine as which is builtin.

In csh , that will be the system which command, which may not do what you want in a few cases. A case where it might make sense to use which is if you want to know the path of a command, ignoring potential shell builtins or functions in bash , csh not tcsh , dash , or Bourne shell scripts, that is shells that don't have whence -p like ksh or zsh , command -ev like yash , whatis -p rc , akanga or a builtin which like tcsh or zsh on systems where which is available and is not the csh script.

Note that if all you want to do is run that echo command, you don't have to get its path, you can just do:. Another case where you may want to use which is when you actually need an external command.

POSIX requires that all shell builtins like command be also available as external commands, but unfortunately, that's not the case for command on many systems. For instance, it's rare to find a command command on Linux based operating systems while most of them have a which command though different ones with different options and behaviours.

Cases where you may want an external command would be wherever you would execute a command without invoking a POSIX shell. The system "some command line" , popen An exception to that would be perl which optimises out the shell if it doesn't see any shell special character other than space.

That also applies to its backtick operator:. The addition of that :; above forces perl to invoke a shell there. By using which , you wouldn't have to use that trick. The reasons why one may not want to use which have already been explained, but here are a few examples on a few systems where which actually fails. On Bourne-like shells, we're comparing the output of which with the output of type type being a shell builtin, it's meant to be the ground truth, as it's the shell telling us how it would invoke a command.

The reason being that which only reports about executables, and sometimes about aliases though not always the ones of your shell , not functions. Another obvious case is builtins or keywords, as which being an external command has no way to know which builtins your shell have and some shells like zsh , bash or ksh can load builtins dynamically :.

The aliases it will report are the ones defined there regardless of the aliases you currently have defined and regardless of the shell you're actually using. On that system, there's an alias defined system-wide that wraps the GNU which command. In my case, sh being dash but it's the same when it's bash.

Which is why which gets it wrong above since it's using dash 's default PATH which is different from ksh93 's. The one Chris is referring to in his answer :.

One thing which from my quick skim it seems that Stephane didn't mention is that which has no idea about your shell's path hash table. This has the effect that it might return a result which is not representative of what actually is run, which makes it ineffective in debugging.

Recently I found this post and was quite surprised -- I have been doing this wrong all the time? But then I checked all the answers and didn't find any reason to drop which entirely. I think that you should use any tool as long as it covers all your needs. I have never heard that. Please provide specific examples. I would worry about your linux distribution and installed software packages, as to that is from where which comes from!

It also provides these other files. Release notes can easily be found back to v2. Not sure what the point or goal of this was, it certainly was not answered in that lengthy thing upvoted times. Sign up to join this community. The best answers are voted up and rise to the top.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Why not use "which"? What to use then? Ask Question. Asked 8 years, 3 months ago.



0コメント

  • 1000 / 1000