7

I just realized that my sysadmin has created a global alias for which:

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

The which manpage just says:

Stop processing options on the right if not on tty.

What does this mean?

Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
RSFalcon7
  • 4,367
  • 6
  • 30
  • 56

2 Answers2

9

Setup:

$ /usr/bin/which --show-dot a
./a
$ /usr/bin/which --show-tilde a
~/a

If you wanted the . version when run interactively, but the ~ version when redirected, you would could use this as an alias:

/usr/bin/which --show-tilde --tty-only --show-dot

Demo:

# interactive / on a tty
$ /usr/bin/which --show-tilde --tty-only --show-dot a
./a
# not interactive / redirected to a file
$ /usr/bin/which --show-tilde --tty-only --show-dot a > output
$ cat output 
~/a

All the options you specify after --tty-only are taken into account only when the output is a tty.

Mat
  • 51,578
  • 10
  • 158
  • 140
  • Nice! I did't realise `which` can do fancy things I always figured it for as dull a tool as there are. – Bananguin Apr 24 '13 at 06:23
  • It is a dull tool on my Ubuntu 13.4 box, it only has the `-a` option – Gerard Apr 24 '13 at 12:28
  • @Gerard: see Sukminder's answer. Debian-based systems don't have those features. There are workarounds in the bug linked. – Mat Apr 24 '13 at 15:00
3

That would mean that if output for which does not refer to a terminal, then do not process --read-alias, --show-dot and --show-tilde.

Typically if to a pipe, ordinary file etc.

which watch | foo # not a tty
which watch > foo # not a tty
which watch       # tty
which watch >&2   # tty

The options are not recognized under e.g. debian:

Runium
  • 28,133
  • 5
  • 50
  • 71