5

By using zsh for some time along with oh-my-zsh framework, I noticed that which command behaves different in zsh, than in bash.

What I mean:

# on zsh
ilias@ilias-pc ~  ➜ which ls
ls: aliased to ls --color=auto
ilias@ilias-pc ~  ➜ which which
which: shell built-in command
ilias@ilias-pc ~  ➜ bash
[ilias@ilias-pc ~]$ which ls
/usr/bin/ls
[ilias@ilias-pc ~]$ which which
/usr/bin/which
[ilias@ilias-pc ~]$ 

Why does this happen and how can I "fix" it?

PS. I reproduce this on Arch Linux (not sure whether it matters but I mention it).

1

2 Answers 2

11

In ZSH, which is equivalent to whence -c (showing function's definitions), not whence -p (which tells executable path). If you want to change that, make an alias.

10
$ zsh -c 'type which'
which is a shell builtin
$ bash -c 'type which'
which is /usr/bin/which

The solution is to not use which(1), which is a non-standard and not very useful command. The question of what you should use instead isn't the most straightforward due to the alternatives being poorly specified and inconsistently implemented, but they are better than which.

Depending on your requirements, you want command (see the -v option), type, or whence. See POSIX for the former two, or your shell manual for the latter. (Bash doesn't support whence, but it is supported by most other ksh derivatives, albeit inconsistently. It typically has the most features).

1
  • Good to know: in zsh documentation all other things, including command -v, are marked as equivalent to whence (possibly with some flags): a perfect example of whence having the most features.
    – ZyX
    Jan 7, 2013 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.