Monday, 30 September 2013

Custom Bash prompt is overwriting itself

Custom Bash prompt is overwriting itself

I'm using custom bash prompt to show git branch.
Everything is in /etc/bash/bashrc:
function formattedGitBranch {
_branch="$(git branch 2>/dev/null | sed -e "/^\s/d" -e "s/^\*\s//")"
test -n "$_branch" && echo -e "\e[0;91m($_branch)" # color before
test -n "$_branch" && echo -e "\e[0;91m($_branch)\e[m" # stop color after
test -n "$_branch" && echo -e "($_branch)" # without color
}
BGreen="\[\033[01;32m\]"
# color is set before function call
PS1=$BGreen'\u@\h\[\033[01;34m\] \w \[\033[0;91m\]$(formattedGitBranch)
\[\033[01;34m\]\$\[\033[00m\] '
# color is set inside function
PS1=$BGreen'\u@\h\[\033[01;34m\] \w $(formattedGitBranch)
\[\033[01;34m\]\$\[\033[00m\] '
Problem is that when I set color for $_branch in the function, my prompt
will be overwrited when EOL is reached:

I solved the problem by setting the colour only in PS1, but still I would
like to know why it is overwriting my prompt in the first case. Is echo -e
involved in this problem somehow?

No comments:

Post a Comment