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

wjandrea
  • 14,109
  • 4
  • 48
  • 98
janw
  • 536
  • 8
  • 24
  • 2
    Please don't post screenshots of text. Copy the text here and apply code formatting instead. – muru Nov 19 '18 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 '18 at 10:18
  • @muru I've added a more compact screenshot. I think it does my my problem more clear. The `$()` does work on other server where I've set the same ps1. – janw Nov 19 '18 at 10:32
  • 2
    The point is that we **don't** want screenshots when normal text will do. https://meta.askubuntu.com/q/8713/158442 – muru Nov 19 '18 at 10:37
  • Please [edit] your question and show us i) where the `__git_ps1` function is defined. Is it even a function or is it a script? Where is it? ii) clarify where you set what. Is your `PS1` being set in `.bash_aliases`? Is your `.bash_aliases` sourced by `.bashrc` (the default in Ubuntu, but perhaps you've changed something)? iii) Also show us the output of `type __git_ps1` – terdon Nov 19 '18 at 11:32
  • @terdon I have updated my question. I can't find the definition, it's just part of git – janw Nov 19 '18 at 11:45
  • No, it isn't part of `git`. You must be defining it somewhere. It may have been installed automatically for you, but it still needs to be sourced somewhere. Please [edit] your question and add the output of `grep __git_ps1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2>/dev/null`. – terdon Nov 19 '18 at 11:51
  • 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_](https://askubuntu.com/a/1044097/504066). – PerlDuck Nov 19 '18 at 11:59
  • @PerlDuck oh, I guess Ubuntu have bundled it with `git`. I meant it isn't part of git itself, although as you say it seems to be part of the git package in Ubuntu. Thanks. – terdon Nov 19 '18 at 12:45
  • I have the `git-sh-prompt`. The function is inside. But I can't find a reference for it. – janw Nov 19 '18 at 13:31
  • 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 '18 at 13:44
  • Yes it's only once, Then it will set my PS1 to a var that does not contain the `__git_ps1`. It's very weird because I've never had this problem before. But it works... Can you add this as a answer? – janw Nov 19 '18 at 13:51
  • Different issue, same solution: [Why is my function not re-evaluated in PS1?](https://askubuntu.com/q/651871/301745) – wjandrea Nov 20 '18 at 19:24

1 Answers1

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.

wjandrea
  • 14,109
  • 4
  • 48
  • 98
  • Is there a way to check this source order? – janw Nov 19 '18 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 '18 at 22:56