I have a script called jsl in /usr/local/bin. Running which jsl finds it there.

I want to temporarily use a different version, and when I echo $PATH I see that /usr/bin is earlier in the path. So I added a script called jsl in that folder and I've verified that it is executable and runs correctly when called with the full path.

However, which jsl still finds the first version, and jsl still runs it.

Why isn't my new jsl being found by which or executed by name?

  • that is weird! I just tested with ls and put in my case in /usr/local/bin and after copying the file which reported the first instance in the $PATH. Using bash 4.2.45 – BitsOfNix Sep 18 '13 at 1:23
  • Can you give the full results for echo $PATH; which -a jsl? – Charles Stewart Sep 18 '13 at 6:58
  • @CharlesStewart - which -a jsl outputs usr/bin/jsl followed by /usr/local/bin/jsl – Nathan Long Sep 18 '13 at 15:57

The shell might be caching the command's location. E.g. zsh does this, and has the rehash command to clear the cache.

  • 6
    in bash and other bourne shells, it's hash -r to clear the path cache. bash's built-in help has more details - type help hash – cas Sep 18 '13 at 2:01
  • hash -r also works in zsh BTW – Romuald Brunet Sep 18 '13 at 12:56
  • That worked! Interestingly, which -a jsl showed both locations in the correct order, but immediately after, which jsl still showed the old one. After hash -r, which jsl showed the new one and jsl ran it. – Nathan Long Sep 18 '13 at 16:00
  • which -a js1 scans the path to find all executables called "js1". This ignores the cache, because the cache is only used to find the one, first "js1", but a scan may include multiple hits. In the case of which js1 it is only looking for the first hit, so can make use of the cache (and therefore, when the cache is out of date, reports the wrong first hit). – David Sainty Sep 23 '13 at 0:17

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