9

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?

msw
  • 10,503
  • 1
  • 31
  • 43
Nathan Long
  • 1,613
  • 1
  • 13
  • 27

1 Answers1

15

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

David Sainty
  • 456
  • 3
  • 4
  • 10
    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 02:01
  • 1
    `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 00:17