0

I found this code in virtualenv activate file:

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands.  Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
    hash -r 2>/dev/null
fi

hash -r 2 gives me an error in both bash and zsh. How does this code work? How does this code forget past commands? Why might $PATH changes not be respected if we don't forget the past commands? Can you show me some example?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
harryhare
  • 33
  • 6

1 Answers1

1

The command isn't

hash -r 2

but rather

hash -r

The 2 is part of a standard error redirect to /dev/null, which essentially throws away error output.

You can read about Bash version of the hash builtin in man bash. Here's a relevant snippet:

Any previously-remembered pathname is discarded. If the -p option is supplied, no path search is performed, and filename is used as the full filename of the command. The -r option causes the shell to forget all remembered locations.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257