源命令后刷新bash提示符

时间:2013-01-01 14:01:16

标签: bash terminal

我使用shell脚本用于某些特定目的,并且它具有的功能是重新.bash_profile

function refresh {
    source "$HOME/.bash_profile"
}

同样.bash_profile有这样的陈述:

if [ -f "$HOME/.bash_prompt" ]; then
    source "$HOME/.bash_prompt"
fi

还应该重新加载.bash_prompt;并且确实如此,该提示文件包含的值应该更改提示的显示(颜色,文本放置等),但这些值不会更改。它们仅在新的终端窗口上更改,或者如果我在终端窗口中明确调用source "$HOME/.bash_prompt"

我在这里做错了吗?

以下是我的.bash_prompt来源:

# Colors
# Bunch of color codes

function print_before_the_prompt {

    # create a $fill of all screen width
    let fillsize=${COLUMNS}
    fill=""
    while [ "$fillsize" -gt "0" ]
    do
    fill="-${fill}" # fill with underscores to work on
    let fillsize=${fillsize}-1
    done

    printf "$txtrst$bakwht%s" "$fill"
    printf "\n$bldblk%s%s\n" "${PWD/$HOME/~}" "$(__git_ps1 "$txtrst [$txtblu%s$txtrst]")"

}

# Load Git completion and prompt
if [ -f "/usr/local/opt/git/etc/bash_completion.d/git-completion.bash" ]; then
    source "/usr/local/opt/git/etc/bash_completion.d/git-completion.bash"
fi
if [ -f "/usr/local/opt/git/etc/bash_completion.d/git-prompt.sh" ]; then
    source "/usr/local/opt/git/etc/bash_completion.d/git-prompt.sh"
fi

GIT_PS1_SHOWDIRTYSTATE=true
PROMPT_COMMAND=print_before_the_prompt
PS1="\[$txtred\]⦿\[$txtrst\] "

1 个答案:

答案 0 :(得分:5)

您还需要获取托管refresh函数的脚本,而不是执行它。如果不这样做,则仅在执行脚本期间更改环境。

解释:当您执行脚本时,它会继承其父级的当前环境(在本例中为您的shell),并且它具有自己的环境。脚本中的所有环境更改仅适用于脚本本身及其子项。

但是,在您提供脚本时,所有更改和命令都会直接影响父级环境。

通常,建议将要使用的脚本与通用脚本分开。例如,您可以拥有一个dev.sh文件,其中包含需要一些特殊变量的特定开发项目的特殊环境变量。

如果您想快速获取当前shell的.bash_profile,可以设置别名。通过执行脚本来完成它是不可能的。

相关问题