`which` may or may not be available on host systems. There are
equivalent which are shell builtin.
This is essentially using the same as https://github.com/apache/maven/commit/8852b87412c0af70efcee76f1bb12bc44635aab1, which has proven to work fine on a large shell variety.