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).
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 thanwhich.Depending on your requirements, you want
command(see the-voption),type, orwhence. 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).