Search code examples
bashgit-bashps1

Why does adding \n at the end of my PS1 lead to substitution errors?


So, I'm on Windows and play around with my PS1 for the Git bash.

My PS1 looks like this:

PS1="\n\[\033[01;35m\]\u@\h\[\033[01;34m\] $PWD \[\033[00m\]\[\033[01;32m\]$(__git_ps1 "(%s)")\[\033[00m\]\nλ "

Then I noticed that the branch name isn't updated when changing branches, and found this answer, which tells me to exchange the double quotes for single quotes.

Now, my PS1 looks like this:

PS1='\n\[\033[01;35m\]\u@\h\[\033[01;34m\] $PWD \[\033[00m\]\[\033[01;32m\]$(__git_ps1 "(%s)")\[\033[00m\]\nλ '

Suddenly, I recieve warnings when executing my .bashrc:

bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `__git_ps1 "(%s)")'

Further investigation shows me that when I remove the \n before the λ at the end of the PS1 definition, the warning disappears.

Can anyone answer me:

  • what does exactly cause this warning (the \n at the end seems totally unrelated to the execution of __git_ps1 to me)?
  • what do I need to change in order to remove the warning?

Edit

When I change the $(__git_ps1 "(%s)") to `__git_ps1 "(%s)"`, the warning is gone and everything works as expected...


Solution

  • Solution

    NEWLINE="
    "
    PS1='\[\033[01;35m\]\u@\h \[\033[01;34m\]$PWD\[\033[01;32m\]$(__git_ps1)\[\033[00m\]$NEWLINEλ '
    

    There is trouble involved in the newline being around, also resetting the color was making the line longer than necessary.

    Smallest sample of problem:

    PS1='$(date)\n$'
    

    Looks like an issue with variable substitution.