The command which in Linux: locate executable commands

Introduction

In the day-to-day of a system manager or developer working on the Linux command line, you often need to know exactly what binary is running when a command is invoked. The shell seeks the executable on the routes defined by the variablePATHbut sometimes there are multiple versions of the same program (for example, one system installation and one in/usr/local/bin) or to confirm that the expected route is being used. The commandwhichrespond to this need quickly and directly, showing the absolute route of the first executable that matches the name provided.

What's the command which?

whichis a utility present in almost all Linux distributions and is part of the set of basic GNU Coreutils tools, although in some systems it can be implemented as a shell script or as a build-in of certain shells (aszsh). Its main function is to search the environment variablePATHthe first executable file whose name matches the argument that is passed on to you and return your full route. If no match is found, it does not produce output and returns a different state code from zero, which allows it to be used in scripts conditionals.

Basic syntax

The simplest way to usewhichis:

which [options] command [comando 2...]

Some useful options are:

  • -a: shows all the coincidences found inPATHNot just the first.
  • -s: silent mode; write nothing on standard output, just return the status code (useful in tests).
  • --version: shows the utility version.
  • --help: shows the short help.

If the option is omitted,whichbehave as if the default search (first match) had been specified.

Practical examples

Let's imagine that we want to know where the binary ofpython3:

which python3

Typical output:

/ usr / bin / python3

If there are several facilities, for example one in/usr/local/bin/python3and another in/usr/bin/python3, using the option-aWe'll see both:

which -a python3

Output:

/ usr / local / bin / python3 / usr / bin / python3

Another common case is to check if a command is available before running it in a script:

if! which rsync > / dev / null; then echo "Error: rsync is not installed" exit 1fi

Here the exit is redirected to/dev/nullBecause we're only interested in the state code.

It is also useful when working with changing development environmentsPATH, likepyenvornvm. For example, after activating a specific version of Node.js withnvm:

nvm use 18which node

The output will show the route under the directory ofnvm, confirming that the correct version is being used.

Limitations and alternatives

Althoughwhichis very practical, has some limitations that it is important to know:

  • It depends exclusively on the variablePATH. If an executable is outside those routes (e.g. called with a relative or absolute route),whichHe won't find it.
  • In some shells,whichmay be an alias or function that masks real behavior, leading to confusing results. Can be verified withtype -a whichto see if it's a build-in or an external executable.
  • It does not show information about execution permits; if the found file does not have the execution bit,whichIt will also show it, although the shell would fail in trying to execute it.

To overcome these constraints, there are more flexible alternatives:

  • command -v: POSX build-in that shows the route or indicates whether it is a function, alias or build-in, without depending on an external executable.
  • type: similar tocommand -vbut in more detail about the type of command (alias, function, build-in, file).
  • whereis: search not only inPATHbut also in source directories and manual pages.
  • findwith expressions likefind /usr -name "python3" -type f -executable: useful when looking at specific locations outside ofPATH.

In practice, combinewhichwithtypeorcommand -vprovides a more complete view of what the shell is really going to run.

Conclusion

The commandwhichis a simple but essential tool for any user of the Linux command line. It allows to quickly locate the executable that the shell will use, verify the presence of programs inPATHand make informed decisions in scripts and interactive sessions. Knowing their syntax, options and limitations, as well as available alternatives, you can manage the working environment more accurately and avoid surprises caused by unexpected versions of the same commands. Incorporatewhichin the daily routine of administration or development contributes to greater clarity and control over the system.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish