6

I want to use the which command to see which executable is actually being invoked, but the defensive noglob aliases keep getting in the way.

Is there a shortcut that I can use to find which executable is invoked when the command is aliased ?

I am using the zsh shell.

tread
  • 356
  • 3
  • 6
  • 21
Ivar
  • 113
  • 1
  • 6

2 Answers2

10

You can use which -a COMMAND (or where COMMAND or whence -ca COMMAND) to find all occurences of COMMAND in the command path.

For example:

% alias ls='noglob ls'
% ls () /bin/ls
% which ls
ls: aliased to noglob ls
% which -a ls
ls: aliased to noglob ls
ls () {
        /bin/ls
}
/bin/ls

As aliases are replaced in the command line before anything is executed, the second command in the list is the one you are looking for (assuming of course that the first line is somehow aliased to the same name)

Adaephon
  • 5,634
  • 3
  • 20
  • 25
  • Can you please explain why does it work? There is no man entry for `where` or `whence`. – peter.babic Sep 15 '20 at 07:13
  • 2
    In *zsh* `which`, `where` and `whence` are all built-in commands, with `which` being equivalent to `whence -c` and `where` being equivalent to `whence -ca`. Their documentation can be found in `man zshbuiltins`. While there is a manpage for `which`, it is not for the built-in, but for the external command (usually `/bin/which` or `/usr/bin/which`). See also the output of the command `which -a which`. – Adaephon Sep 23 '20 at 15:06
2

Use which -p e.g.:

✗ which ls
ls: aliased to ls --color=tty
✗ which -p ls
/bin/ls

From https://linux.die.net/man/1/zshbuiltins:

(whence) -p

Do a path search for name even if it is an alias, reserved word, shell function or builtin.