Windows Git bash prompt not displaying colours from a function

狂风中的少年 提交于 2019-12-23 03:25:20

问题


I'm using the following colour codes/function (pinched from Paul Irish) in my .bash_prompt, to display branch name and status in Git bash on windows.

MAGENTA="\033[1;31m"
ORANGE="\033[1;33m"
GREEN="\033[1;32m"
PURPLE="\033[1;35m"
WHITE="\033[1;37m"
BOLD=""
RESET="\033[m"

function git_info() {

        # check if we're in a git repo
        git rev-parse --is-inside-work-tree &>/dev/null || return

        # quickest check for what branch we're on
        branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')

        # check if it's dirty (via github.com/sindresorhus/pure)
        dirty=$(git diff --quiet --ignore-submodules HEAD &>/dev/null; [ $? -eq 1 ]&& echo -e "*")

        echo $WHITE" on "$PURPLE$branch$dirty
}

The function displays the information properly, but instead of colouring the two parts it prints out the colour codes like this:

\[\033[1;37m\] on \[\033[1;35m\]working*
$

This is using the following (reduced to illustrate the problem) PS1 line:

PS1="\$(git_info)\n$ "

I should note that colours work fine if the codes are added directly to the PS1 line i.e:

PS1="\[$PURPLE\]\w\n$ \[$RESET\]"

I'm feeling my way around all this bash customisation, so might be doing something obviously stupid!


回答1:


Maybe echo -e. From man echo:

-e enable interpretation of backslash escapes

Also, you can use variables inside double quotes:

echo "$WHITE on $PURPLE$branch$dirty"


来源:https://stackoverflow.com/questions/19752534/windows-git-bash-prompt-not-displaying-colours-from-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!