2

I have this custom .profile script:

PS1='\[\033]0;WSL2 Bash\W\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[36m\]'       # change to green
PS1="$PS1"'bash@bexgboost '         # user@host<space>
PS1="$PS1"'\[\033[31m\]'       # change to brownish yellow
PS1="$PS1"'\W'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[35m\]'  # change color to cyan
        PS1="$PS1"'`__git_ps1`'   # bash function
    fi
fi

PS1="$PS1"'[\033[0m]' # change color PS1="$PS1"'\n' # new line PS1="$PS1"'$ ' # prompt: always $

Currently, it looks like this when I launch the terminal:

enter image description here

The script was supposed to show the branch name like this:

enter image description here

Ignore the mismatch in dir names.

What am I doing wrong?

pa4080
  • 30,621
Bex T.
  • 123
  • 1
  • 5

1 Answers1

3

The problem in your code is at line 18:

PS1="$PS1"'`__git_ps1`'   # bash function

Here you are using command substitution (old style by backticks) where you calling the function __git_ps1 and it should be replaced by its output, but I can't see the definition of the function in your example code. So I think you can add the following function definition somewhere before that line:

__git_ps1() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

Then save and source the profile file - . .profile, and it should work.


In addition, here is a working example, where the default ~/.bashrc file (I prefer to edit this file instead of .profile in this case) of Ubuntu Server 22.04 is changed to be achieved almost the same as your desired look. Note here I'm using the new syntax of command substitution $(). And the function parse_git_branch() is defined before its calling.

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

if [ "$color_prompt" = yes ]; then #PS1='${debian_chroot:+($debian_chroot)}[\033[01;32m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]' # green #PS1='${debian_chroot:+($debian_chroot)}[\033[01;33m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]' # yellow #PS1='${debian_chroot:+($debian_chroot)}[\033[01;34m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]' # blue PS1='${debian_chroot:+($debian_chroot)}[\033[01;35m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]' # purple #PS1='${debian_chroot:+($debian_chroot)}[\033[01;31m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]' # red

 PS1=&quot;${PS1}\[\033[01;33m\]\$(parse_git_branch)\[\033[0m\]\n&quot; # HERE WE CALL THE FUNCTION THAT PARSE THE BRANCH
 PS1=&quot;${PS1}\$ &quot;

else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ ' fi

Only the relevant lines of .bashrc are shown. For the default state find the statement if [ "$color_prompt" = yes ]; (around line 70) in your .bashrc.

Here is how it looks in practice:

enter image description here

  • Note I'm using GNOME terminal within Kali Linux to connect to Ubuntu Server and this is the reason why the colors are slightly different from these that will be produced by the same code on Ubuntu.

References:

pa4080
  • 30,621