注意环境变量的变化 - ZSH

Watch for environment variable change - ZSH

有没有办法在 zsh/bash 中监视环境变量的变化?例如,当切换我的 kubernetes 环境时,我希望能够读取设置的变量并对我的终端进行更改 window 如果我在生产与开发等

我们切换环境的方式是我们工具的一部分。我希望能够在我自己的机器上扩展它而无需更新任何工具。如果无法观察环境变量的变化,我也在寻找一种使用类似于 builtin.

的方法

Example: create a function of the same name as an alias, call that alias from within the function, then do some other action afterward.

这两种 shell 都提供了一种在显示提示之前执行任意代码的方法;您可以使用它来检查特定变量的值并采取适当的措施。

.bashrc中:

# The function name doesn't matter; it's just easier
# to set PROMPT_COMMAND to the name of a function than
# to arbitrary code.
pre_prompt () {
    if [[ $SOME_VAR == "prod" ]]; then
        doSomething
    else [[
        doSomethingElse
    fi
}

PROMPT_COMMAND=pre_prompt

.zshrc中:

precmd () {
    if [[ $SOME_VAR == "prod" ]]; then
        doSomething
    else [[
        doSomethingElse
    fi
}