Bash – Difference between “command not found” and “no such file or directory”

bashcommand lineexecutable

For example:

$ node
-bash: /usr/local/bin/node: No such file or directory
$ foo
-bash: foo: command not found

What's the difference? In both cases, node and foo are invalid commands, but it seems like Unix just can't find the node binary? When uninstalling a program, e.g. node, is there a way to clean this up so that I get

$ node
-bash: node: command not found

EDIT:

Results from type command:

$ type node
node is hashed (/usr/local/bin/node)
$ type foo
-bash: type: foo: not found

Best Answer

That's because bash remembered your command location, store it in a hash table.

After you uninstalled node, the hash table isn't cleared, bash still thinks node is at /usr/local/bin/node, skipping the PATH lookup, and calling /usr/local/bin/node directly, using execve(). Since when node isn't there anymore, execve() returns ENOENT error, means no such file or directory, bash reported that error to you.

In bash, you can remove an entry from hash table:

hash -d node

or remove the entire hash table (works in all POSIX shell):

hash -r
Related Question