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,504
janw
  • 556

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,504