While in Cygwin
and nearly every other *nix, I have used the following code within my .bashrc
, for years.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\]\u@\h\[\033[00m\] \[\033[00;33m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\] \n\$ '
which generate a colored prompt when in a git folder.
# xxxx@LAPTOP /cygdrive/c/dev/some_git_repo (master)
However, trying the same in any of the MINGW64, MSYS, UCRT64 fails, to inject the parse_git_branch
result. It seem that those shell simply refuse to execute the function. So clearly MSYS devs are doing something weird here. (I also noticed there are other weirdness in interpreting single and double quotes.)
I also tried:
# PS1='\[\e]00;\w\a\]\n\[\e[00;32m\]\u@\h \[\e[00;35m\]$MSYSTEM\[\e[00m\] \[\e[00;33m\]\w\[\e[01;31m\] $(parse_git_branch)\[\e[00m\] \n\$ '
# Error:
# -bash: command substitution: line 1: syntax error near unexpected token `)'
# -bash: command substitution: line 1: `parse_git_branch)'
Q: How can I restore showing the git branch I am in, in these shells?
You can use PROMPT_COMMAND
. Check out the Bash-Prompt-HOWTO.
PROMPT_COMMAND='PS1_GIT_BRANCH=$(parse_git_branch)'
PS1='...${PS1_GIT_BRANCH}...'