简体   繁体   English

`hash -r 2` 有什么作用?

[英]What does `hash -r 2` do?

I found this code in virtualenv activate file:我在 virtualenv activate文件中找到了这段代码:

# 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. hash -r 2在 bash 和 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?如果我们不忘记过去的命令,为什么$PATH更改可能不被尊重? Can you show me some example?你能给我举个例子吗?

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. 2重定向/dev/null的标准错误的一部分,它基本上会丢弃错误 output。

You can read about Bash version of the hash builtin in man bash .您可以阅读有关 Bash 版本的hash内置在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.如果提供了 -p 选项,则不执行路径搜索,并将文件名用作命令的完整文件名。 The -r option causes the shell to forget all remembered locations. -r 选项导致 shell 忘记所有记住的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM