__git_ps1 not found but does exist
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.
Related videos on Youtube
janw
Updated on September 18, 2022Comments
-
janw over 1 year
~/.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 matchessudo grep 'git-sh-prompt' -nr /
What is wrong here?
-
muru over 5 yearsPlease don't post screenshots of text. Copy the text here and apply code formatting instead.
-
muru over 5 yearsAlso, if you used double quotes,
$(__git_ps1)
will be evaluated whenPS1
is set, not whenPS1
is used (i.e., when the prompt is printed). -
janw over 5 years@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. -
muru over 5 yearsThe point is that we don't want screenshots when normal text will do. meta.askubuntu.com/q/8713/158442
-
terdon over 5 yearsPlease 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 yourPS1
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 oftype __git_ps1
-
janw over 5 years@terdon I have updated my question. I can't find the definition, it's just part of git
-
terdon over 5 yearsNo, 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 ofgrep __git_ps1 ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2>/dev/null
. -
PerlDuck over 5 yearsYes, @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. -
terdon over 5 years@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. -
janw over 5 yearsI have the
git-sh-prompt
. The function is inside. But I can't find a reference for it. -
PerlDuck over 5 yearsYou 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 sourcinggit-sh-prompt
or your file~/.local/git-completion.bash
(shown in an older version of your post)). -
janw over 5 yearsYes 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? -
wjandrea over 5 yearsDifferent issue, same solution: Why is my function not re-evaluated in PS1?
-
-
janw over 5 yearsIs there a way to check this source order?
-
wjandrea over 5 years@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