0

After trawling google, the bash manpage and other questions here on SO, I have found similar posts but I have failed to determine the cause of this particular issue.

I have installed git-prompt.sh locally. All is good in the bash --login shell. It really is a good thing on both Windows and Linux.

However, as soon as I execute a bash subshell (e.g so that I can temporarily modify my environment etc) I get the error:

$ ... <in/out of repos and all good>
$ bash
bash: __git_ps1: command not found

This problem does not happen on Windows.

Something to note is that I have my dotfiles setup via git. For this reason I can run my configuration on both windows and linux and they both have exactly the same git-prompt.sh file.

  • Windows 10 git==2.19.2.windows.1 bash==4.4.19(2)-release
  • Arch Linux git==2.19.2 bash==4.4.23(1)-release

Both are fresh OS installs.

My best guess so far is that different shell interaction semantics are executed when operating under windows rather than linux. I have experimented with various bash invocations (--login, --norc ie so I can manually set stuff) etc. There are 3 reasons for this suspicion:

  1. This error looks like the kind of error we get when we forget to export a variable/parameter.
  2. Both "/etc/profile.d" sources are different on each platform. Some contain git support, and perhaps this is contributing to the previous reason.
  3. My bash-fu was good ~10+ years ago, now I am rusty.

One thing I know for sure is that the first login bash ("sub")shell works perfectly on both platforms.

Any ideas, feedback or pointers?

Thanks,

Matt

SerialPrimate
  • 11
  • 1
  • 5
  • 1
    Looks like you defined but didn't export the `__git_ps1` function in your parent shell, but *are* exporting a `PS1` that refers to that function... so child processes get the `PS1` that only works with the function, but don't get the function itself. Putting `export -f __git_ps1` alongside the assignment should fix the issue, or moving the function declaration from your `.bash_profile` to your `.bashrc`. – Charles Duffy Nov 28 '18 at 21:11
  • 1
    BTW, for future reference, our sister site [unix.se] is generally a better fit for questions about dotfiles (or otherwise interactive shell setup, vs software development proper). – Charles Duffy Nov 28 '18 at 21:12
  • Where are you sourcing `git_prompt.sh`? You should probably do it in `.bashrc`, not `.bash_profile` or some other file that is only used for login shells, rather than any interactive shell. – chepner Nov 28 '18 at 21:17
  • @CharlesDuffy I guess Unix & Linux will tolerate the Windows reference! ;-) – SerialPrimate Nov 29 '18 at 07:58
  • @chepner Thanks for the suggestion. I have implemented in .bashrc as forwarded from .bash_profile as the initial shell is a "--login" shell. As I understand this is a standard practice. – SerialPrimate Nov 29 '18 at 08:00
  • 1
    Not really. It's better to set `PS1` on demand in `.bashrc` rather than cluttering your environment with it, because no other program aside from an interactive shell cares about its value. – chepner Nov 29 '18 at 12:07
  • (OK, other shells might care about `PS1`, but considering you are almost certainly relying on `bash`-specific features in your prompt, having a different shell inherit such a prompt won't work as expected.) – chepner Nov 29 '18 at 12:09
  • @chepner Thanks for your help. For my previous comment to you, my understanding is as per [https://www.stefaanlippens.net/bashrc_and_others/](https://www.stefaanlippens.net/bashrc_and_others/) – SerialPrimate Nov 30 '18 at 08:59
  • I want to set up git-prompt in my .bash_profile and not .bashrc because it is slow, and I don't want to have to wait each time something creates an interactive sub-shell. I also had to add export -f __git_sequencer_status and __git_eread. – Lawrence I. Siden Dec 03 '19 at 17:23

2 Answers2

0

This looks like your PS1 (or something performing a similar function) is exported, but your __git_ps1 function itself is not.

To export the function, use export -f __git_ps1.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Solved. The issue was due to a comibination of (2) above (i.e. indirect different versions of the prompt) and bad quoting of a regular expression by myself in a guard.

Thank you for your help.

SerialPrimate
  • 11
  • 1
  • 5