5

~/.bash_aliases where I set PS1, and is included in ~/.bashrc (the default settings)

# color PS1
PS1="\[\033[01;90m\]\D{%H:%M} \[\033[01;33m\]Ubuntu\[\033[00m\] \[\033[01;34m\]\w\[\033[01;35m\]$(__git_ps1) \[\033[01;36m\]\$\[\033[00m\] "

But when I start a terminal I get error __git_ps1: command not found

But when I run the function manual $ __git_ps1 in a git folder it does echo the current branch.

Also when I manually run
$ PS1="\[\033[01;90m\]\D{%H:%M} \[\033[01;33m\]Ubuntu\[\033[00m\] \[\033[01;34m\]\w\[\033[01;35m\]$(__git_ps1) \[\033[01;36m\]\$\[\033[00m\] "

the PS1 gets updated and __git_ps1 part does get added.

I did not install it myself. I only installed git.
sudo apt install -y git (git version 2.19.1)

__git_ps1 is defined in /usr/lib/git-core/git-sh-prompt (the file on github)

grep __git_ps1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2>/dev/null

Only the .bash_aliases file shows up.
A full grep of git-sh-promt only returns binary matches

sudo grep 'git-sh-prompt' -nr /

What is wrong here?

PS1 weirdness

13
  • 2
    Please don't post screenshots of text. Copy the text here and apply code formatting instead.
    – muru
    Nov 19, 2018 at 10:17
  • Also, if you used double quotes, $(__git_ps1) will be evaluated when PS1 is set, not when PS1 is used (i.e., when the prompt is printed).
    – muru
    Nov 19, 2018 at 10:18
  • 2
    The point is that we don't want screenshots when normal text will do. meta.askubuntu.com/q/8713/158442
    – muru
    Nov 19, 2018 at 10:37
  • 1
    Yes, @terdon, __git_ps1 does come with git, in particular with the file /usr/lib/git-core/git-sh-prompt. See my answer to the related question script to show git branch in bash no longer works on ubuntu 18.04.
    – PerlDuck
    Nov 19, 2018 at 11:59
  • 1
    You get that command not found only once, right? Not for every single prompt in the terminal? I'd guess the usage of __git_ps1 in the definition of $PS1 comes before the function gets defined (e.g. by sourcing git-sh-prompt or your file ~/.local/git-completion.bash (shown in an older version of your post)).
    – PerlDuck
    Nov 19, 2018 at 13:44

1 Answer 1

6

Using a de-colorized version for clarity:

PS1="\D{%H:%M} Ubuntu \w$(__git_ps1) \$ "

The double quotes tell Bash to evaluate what is between the quotes, including $(__git_ps1), but /usr/lib/git-core/git-sh-prompt hasn't been sourced yet, hence the error.

Simply change it to use single quotes, which will prevent $(__git_ps1) from being evaluated until the PS1 is evaluated (i.e. when the interactive shell is ready for input and shows you the prompt).

PS1='\D{%H:%M} Ubuntu \w$(__git_ps1) \$ '

Escaping the dollar sign also works, but it's harder to read:

PS1="\D{%H:%M} Ubuntu \w\$(__git_ps1) \$ "

By the way, ~/.bash_aliases is intended for shell aliases, so it's a weird place to put your PS1. Personally I would put it in ~/.bashrc instead.

2
  • Is there a way to check this source order?
    – janw
    Nov 19, 2018 at 21:46
  • 1
    @janw for source order, the closest you could get is bash -x, which is pretty verbose, but come to think of it, this could also help diagnose the original problem, cause it will show every command it executes during startup
    – wjandrea
    Nov 19, 2018 at 22:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .