Where is the `which` command defined?

Fedora uses a redefinition of the which command, which is pretty annoying as it breaks my openwrt build. The executable at /usr/bin/which looks fine, but when I run which which I get

which ()
{
    ( alias;
    eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}

and that seems to break scripts which check if which is installed.
So, how do I make which behave normally?

It is part of the which package and is defined in /etc/profile.d/which2*

To work around it you have a bunch of options:

  • Call /usr/bin/which directly.
  • alias which to /us/bin/which
  • redefine the function which() to just call which without all those arguments
  • Use unset -f which to undeclare the function.

In your case, the last option is probably what you want.

That being said, if a bash function is breaking detection of which in openwrt that is probably a bug that should be fixed in openwrt.

6 Likes

Thanks, the last option is indeed the most convenient for me and works very well.

While I agree with you, I admit that there could be differing opinions.
Openwrt uses

$(eval $(call SetupHostCommand,which,Please install 'which', \
       which which | grep which))

in some makefile(?). Not sure how that could be rewritten in a way to work reliably across distributions which all might have different declarations of which and without introducing new dependencies just to check for the build requirements.

1 Like

Glad it worked for you!

You can use env for that.

env which which | grep which

3 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.