brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: hash -r makes the shell forget where an old program lived

Shell
~/tips/tip-shell-hash-r-forget-path

hash -r # forget everything

Fix 'No such file or directory' after moving or reinstalling a program.

After you install a newer version of a tool in another folder, the shell can keep running the old one, or complain that a file is missing while which shows it right there. That is the hash table at work.

Bash remembers the full path of every command you ran, so it does not search PATH each time. hash shows that list, and hash -r clears it:

hash        # what bash remembers
hash -t node
hash -r     # forget everything
node --version

After hash -r the next call searches PATH again and finds the new location. hash -d node forgets only that one name.

This mostly bites when a program moves from /usr/bin to /usr/local/bin, or when you put a new folder in front of PATH in a running session. If the remembered file is gone, bash does not look further by default and just fails with No such file or directory. A new copy earlier in PATH also stays hidden until you clear the table. shopt -s checkhash makes bash search again when a remembered file has disappeared.

I can see why people forget this exists and open a new terminal instead, which does the same thing, since every new shell starts with an empty table. In zsh the matching command is rehash, and hash -r works there too.

Further reading: Bash manual: Bourne Shell Builtins (hash)

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link