Which

Show the full path of commands

Edition 2.12, for Which Version 2.12

6 July 2000

Carlo Wood <carlo@gnu.org">carlo@gnu.org>


Copyright (C) 2000, by

Carlo Wood, Run on IRC <carlo@alinoe.com> RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61

The @command{which} Program

@command{Which} takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable @env{PATH} using the same algorithm as @command{bash(1)}.

Invoking @command{which}

The synopsis to invoke @command{which} is

which [options] [--] programname [...]

Option Summary

`--all'
`-a'
Print all matching executables in @env{PATH}, not just the first.
`--read-alias'
`-i'
Read aliases from stdin, reporting matching ones on stdout. This is useful in combination with using an alias for which itself. For example
alias which='alias | which -i'.
`--skip-alias'
Ignore option @option{--read-alias}, if any. This is useful to explicity search for normal binaries, while using the @option{--read-alias} option in an alias for which.
`--skip-dot'
Skip directories in @env{PATH} that start with a dot.
`--skip-tilde'
Skip directories in @env{PATH} that start with a tilde and executables which reside in the @env{HOME} directory.
`--show-dot'
If a directory in @env{PATH} starts with a dot and a matching executable was found for that path, then print "./programname" rather than the full path.
`--show-tilde'
Output a tilde when a directory matches the @env{HOME} directory. This option is ignored when which is invoked as root.
`--tty-only'
Stop processing options on the right if not on tty.
`--version, -v, -V'
Print version information on standard output then exit successfully.
`--help'
Print usage information on standard output then exit successfully.

Return Value

@command{Which} returns the number of failed arguments, or -1 when no `programname' was given.

Example

A useful way to use this utility is by adding an alias for @command{which} like the following:

alias which='which --tty-only --show-tilde --show-dot'

This will print the readable ~/ and ./ when starting which from your prompt, while still printing the full path when used from a script:

> which q2
~/bin/q2
> echo `which q2`
/home/carlo/bin/q2

Aliases are also supported, through the use of an alias for which itself. An example alias for which that is using this feature is as follows:

alias which='alias | which --tty-only --read-alias --show-tilde --show-dot'

This will print the output of alias for each alias that matches one of the given arguments. For example, using this alias on itself in a tcsh:

$ alias which alias \| /usr/bin/which -i !\*
$ which which
which   (alias | /usr/bin/which -i !*)
	/usr/bin/which

Bugs

The @env{HOME} directory is determined by looking for the @env{HOME} environment variable, which aborts when this variable doesn't exist. @command{Which} will consider two equivalent directories to be different when one of them contains a path with a symbolic link.

See Also

@command{bash(1)}

Index

Jump to: @ - a - b - c - d - e - i - o - r - s

@

  • @option{--all}
  • @option{--help}
  • @option{--read-alias}
  • @option{--show-dot}
  • @option{--show-tilde}
  • @option{--skip-alias}
  • @option{--skip-dot}
  • @option{--skip-tilde}
  • @option{--tty-only}
  • @option{--version}
  • @option{-a}
  • @option{-i}
  • @option{-V}
  • @option{-v}
  • a

  • aliases, handling of
  • b

  • Bugs, known
  • c

  • Command line Options
  • d

  • Description of @command{which}
  • e

  • Examples
  • i

  • Invoking @command{which}
  • o

  • Options, command line
  • Overview of command line options
  • r

  • Return value of @command{which}
  • s

  • Synopsis

  • This document was generated on 4 November 2000 using texi2html 1.56k.