问题
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