Mac OSX | Bash .bash_profile 未更新 PS1

Mac OSX | Bash .bash_profile not updating PS1

我有一个名为 .bash_prompt 的小脚本,由 ~/.bash_profile.

中的 source ~/.bash_prompt 调用

该脚本设置我的 PS1 以显示有关当前 git 存储库的一些有用信息。 不幸的是,git-部分仅在生成新终端时才执行,因此只有在我更改为 git 存储库后手动调用脚本时才会显示分支。

如何在每次执行命令时更新 bash 提示符?

function git_branch() {
    local GITDIR=$(git rev-parse --show-toplevel 2>&1)
    if [[ "$GITDIR" != '/Users/\u' ]]
    then
        local BRANCH=`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'`
      if [ -n "$BRANCH" ]; then
          echo -e "$BRANCH"
      fi
    else
     echo ""
    fi
}

function git_prompt() {
    local prompt_unpushed_symbol="△"
    local prompt_unpulled_symbol="▽"
    local prompt_dirty_symbol="*"
    local prompt_synced_symbol="✓"

    local local_branch=$(git_branch)
    local remote_branch="origin/$local_branch"
    local first_log="$(git log $local_branch $remote_branch -1 2> /dev/null)"

    local STATUS=`git status 2>&1`
    if [[ "$STATUS" == *'Not a git repository'* ]]; then
        echo ""
    elif [[ "$STATUS" != *'working directory clean'* ]]; then
        echo "[$local_branch $prompt_dirty_symbol]"
    elif [[ "$STATUS" == *'Your branch is ahead'* ]]; then
        echo "[$local_branch $prompt_unpushed_symbol]"
    elif [[ -n "$first_log" ]]; then
        echo "[$local_branch $prompt_unpulled_symbol]"
    else
        echo "[$local_branch $prompt_synced_symbol]"
    fi
}

function colorPrompt {
    local c_brace="\[3[m\]"
    local c_git="\[3[31m\]"

    local user_host="\[3[36m\]\u\[3[m\]@\[3[32m\]\h"
    local location="\[3[33;1m\]\w"
    local tail="\n$ "

    export PS1="[$user_host $location$c_brace]$c_git$(git_prompt)$c_brace$tail"
}

colorPrompt

PROMPT_COMMAND shell 变量的值在显示提示之前执行;此功能的主要用途之一是设置 PS1 的值。在你的情况下,你需要做的就是添加

PROMPT_COMMAND=color_prompt

采购 .bash_prompt 后到您的 .bash_profile