Use a shell builtin of posix shells insteaf of calling 'which'. See also http://stackoverflow.com/a/677212