窗户的Git bash提示符不是从一个函数显示颜色 [英] Windows Git bash prompt not displaying colours from a function
问题描述
我用下面的颜色codeS /功能(保罗爱尔兰捏)在我.bash_prompt,显示在Windows的Git的bash支行名称和状态。
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
}
该功能显示的信息正确,但而不是着色两部分它打印出来的颜色codeS这样的:
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*
$
这是利用以下的(减少来说明这个问题)PS1行:
This is using the following (reduced to illustrate the problem) PS1 line:
PS1="\$(git_info)\n$ "
我要指出,如果codeS直接添加到PS1线即颜色做工精细:
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!
推荐答案
也许回声-e
。从人回声
:
-e使反斜杠间pretation逃逸
此外,您还可以使用双引号里面的变量:
Also, you can use variables inside double quotes:
echo "$WHITE on $PURPLE$branch$dirty"
这篇关于窗户的Git bash提示符不是从一个函数显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!