27

When I do

which pip3

I get

/usr/local/bin/pip3

but when I try to execute pip3 I get an error as follows:

bash: /usr/bin/pip3: No such file or directory

This is because I recently deleted that file. Now which command points to another version of pip3 that is located in /usr/local/bin but the shell still remembers the wrong path. How do I make it forget about that path?

The which manual says

which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in
       a strictly POSIX-conformant shell.  It does this by searching the PATH for executable files matching the names of the arguments. It does not follow
       symbolic links.

Both /usr/local/bin and /usr/bin are in my PATH variable, and /usr/local/bin/pip3 is not a symbolic link, it's an executable. So why doesn't it execute?

  • What is the content of /usr/local/bin/pip3? – Thomas Jan 8 '17 at 16:13
  • Had you previously run pip3 in that shell while it was in /usr/bin and then moved it? – Eric Renouf Jan 8 '17 at 16:14
  • 1
    What do you see if you run hash -t pip3? – Eric Renouf Jan 8 '17 at 16:16
  • 1
    @Eric Renouf hash -t pip3 prints /usr/bin/pip3 – spiderface Jan 8 '17 at 16:24
  • 2
    Unless you have a very good reason, you should always use type eather than which. type is built into a posix shell and tells you what a shell will do, rather than which which tries to guess what the shell will do. – icarus Jan 8 '17 at 21:23
34

When you run a command in bash it will remember the location of that executable so it doesn't have to search the PATH again each time. So if you run the executable, then change the location, bash will still try to use the old location. You should be able to confirm this with hash -t pip3 which will show the old location.

If you run hash -d pip3 it will tell bash to forget the old location and should find the new one next time you try.

  • 4
    Or hash -r to clear the whole table. – mattdm Jan 8 '17 at 16:27
  • Yes, that worked. I think I'll change the title of the question because it has nothing to do with which. – spiderface Jan 8 '17 at 16:27
  • 1
    @spiderface for most bash features it's easier to use help instead of man, so here help hash – Eric Renouf Jan 8 '17 at 21:58
  • 3
    @spiderface type hash will tell you that it's a shell builtin, so it doesn't have its own man page. Instead, use help hash or look up hash in bash's man page. – deltab Jan 8 '17 at 23:14
  • 1
    Or, if you really want to use man...since hash is a bash builtin, you would want man bash to find it. But what the man page says is essentially what help hash to bash says. – MAP Jan 8 '17 at 23:32

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.