I have a shell script called foo.sh in /usr/local/sbin. Besides I use another script with the same name. It is stored in /home/user/scripts.

Both paths belong to PATH. How can I find out, which one of those scripts are executed when typing foo.sh on the command line? Can I influence it?


A few days ago I was satisfied with my answer. I also knew that the which command is not used to test whether a command exists. Now I'm not so satisfied with my answer, and I refer to this great response.

With a simple which command:

which foo.sh

Or a type command:

type foo.sh

And you can influence this with the order of the paths in your $PATH or via an absolute path when the script starts, E.G.



If PATH is defined as below


And you start a script or something else which is in /usr/local/bin and in /usr/bin, the script in /usr/local/bin as a higher priority.

  • 1
    Speaking of bash and zsh there's also the type built-in; instead in zsh which, differently from bash, is a built-in (and not /usr/bin/which) – kos Sep 11 '15 at 13:15
  • 1
  • @muru thanks. A little I knew about it already, but not in such detail. I've added my views to my answer. Thanks again. – A.B. Sep 19 '15 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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