3

I tried to integrate the current git branch in my prompt, but it doesn't behave as expected. I'm using bash on ubuntu 16.04 and git 2.7.4. When I start a terminal nothing of git is shown. If I source my .bashrc from inside a repository the branch is shown, but doesn't update anymore. This is what I wrote in my .bashrc:

green="\[\033[01;32m\]"
blue="\[\033[01;34m\]"
no_color="\[\033[00m\]"
purple="\[\033[01;35m\]"

source ~/.git-prompt.sh
export PS1="$purple\u $green$(__git_ps1 " (%s)") $blue\W $no_color \$ "

Update:

I tried follow the instructions in git-prompt but still the same result. However if I just copy the suggestion from git-prompt: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' it works, but of course doesn't look like I want. Is there some syntax error I'm missing? The template PS1 works even if don't use export, just PS1=...

3

That's because .bashrc is read only once: when you start a new interactive non-login shell (when you open a new terminal, for example). So the PS1 is set the first time you open the terminal and is never changed.

The right way of doing this (assuming your git-prompt and __git_ps scripts work as expected) is to use PROMPT_COMMAND. As explained in man bash:

   PROMPT_COMMAND
          If set, the value is executed as a command prior to issuing each
          primary prompt.

You want __git_ps1 to be run before each prompt is shown (so after every command you run; after a cd, for instance). Therefore, you need to put it into PROMPT_COMMAND. If your __git_ps1 is what I think it is, it will be setting PS1 for you. You just need to run it each time a prompt is shown. So, add this line to your ~/.bashrc:

PROMPT_COMMAND="$purple\u $green$(__git_ps1 "(%s)") $blue\W $no_color \$ "

Finally, I can't be sure since you're not showing the relevant scripts, but I very much doubt that the (%s) makes sense there. That will just print the string %s. If you need more help, please edit your question and i) include both __git_ps1 and /etc/bash_completion.d/git-prompt (or tell us how you installed them if they came from an Ubuntu package) and ii) show us what your prompt is supposed to look like.

  • I updated my question. I know that .bashrc is only read once, but nonetheless the prompt should update every time. – Pepe Mar 24 '17 at 16:46
  • @Pepe it won't unless you use PROMP_COMMAND as I explain in my answer. Did you try it? – terdon Mar 24 '17 at 16:58
  • thanks for your answer, but you're on the wrong track. Also the %s is not needless. I posted the solution below. – Pepe Mar 25 '17 at 9:25
2

Ok I found the solution. It is not necessary to use prompt command. The bug arises due to a syntax error.

If I assign the content of PS1 with "" in order to use my variables for the colors, it only executes the __git_ps1 function when .bashrc is sourced.

But when I assign the PS1 content within '' and do it without variables for the color and instead write the codes out, it works as expected. And as I read here it seems to be better practice not to export PS1 to the environment.

So the solution looks like:

PS1='\[\033[01;35m\]\u \[\033[01;32m\]$(__git_ps1 " (%s)") \[\033[01;34m\]\W \[\033[00m\] \$ '

However I would be curious to know, what's the reason for this. It works within "" in macOS.

  • It does work, thanks. However it's a pain in the neck when you have the colors set as variables. – Paulo Pedroso Oct 2 '18 at 12:29
  • glad it works for you. However, in the meantime I switched the shell to zsh. zsh in combination with oh-my-zshell is just so much nicer and more convenient. I'd recommend to use zsh and choose a theme for the shell prompt you like. – Pepe Oct 3 '18 at 13:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.