【发布时间】:2013-09-30 10:57:24
【问题描述】:
我正在使用自定义 bash 提示符来显示 git 分支。
一切都在/etc/bash/bashrc:
function formattedGitBranch {
_branch="$(git branch 2>/dev/null | sed -e "/^\s/d" -e "s/^\*\s//")"
# tried these:
echo -e "\e[0;91m ($_branch)"
echo -e "\e[0;91m ($_branch) \e[m"
echo -e $'\e[0;91m'"($_branch)"
echo "($_branch)"
echo "$(tput setaf 2) ($_branch) $(tput setaf 9)"
printf "\e[0;91m ($_branch)"
}
# color is set before function call
PS1='\[\033[01;34m\] \[\033[0;91m\]$(formattedGitBranch) \$\[\033[00m\] '
# color is set inside function
PS1='\[\033[01;34m\] $(formattedGitBranch) \$\[\033[00m\] '
问题是当我在函数中为$_branch设置颜色时,到达EOL时我的提示将被覆盖:
尝试了所有可能的变体tput、printf、$'' 表示法。
我通过只在PS1中设置颜色解决了这个问题:
但是..
- 我想知道为什么它会覆盖我的提示
- 使用函数时如何解决此问题
我正在使用 Gentoo Linux。 GNU bash, verze 4.2.37(1)-release (i686-pc-linux-gnu)
【问题讨论】:
-
我在我的 Windows 机器上使用它。你能在 GitHub Gist 的评论中解决我的问题吗? gist.github.com/dannguyen/d1734b252df2559c625a
标签: bash command-prompt gentoo