0

I'm in Linux Mint 20.1 Cinnamon, not that it matters this time,

I have trouble with customizing my PS1, or generally Bash prompt to colorize the last char $ (user); # (root).

Current PS1 code in .bashrc:

if [ "$color_prompt" = yes ]; then
    color_red=$(tput bold)$(tput setaf 1)
    color_green=$(tput bold)$(tput setaf 2)
    color_reset=$(tput sgr0)
    PS1='${debian_chroot:+($debian_chroot)}\u @ \h $PWD $color_green\$$color_reset '
else
    PS1='${debian_chroot:+($debian_chroot)}\u @ \h $PWD \$ '
fi

Problem description:

When I launch the terminal (gnome-terminal), it looks just fine, but if I go up with an arrow key for history, the text gets garbled after about 10+ ups:

garbled terminal example

I can't explain it, can you? What am I doing wrong?

PS: I am unsure why it is in single quotes, and if I can just change it to double quotes...


Fail #2

Have just tried to "convert" it to double-quoted format, which leads again to troubles when upping history a dozen or more times.

PS1="\u @ \h \[\$PWD\] \[$(tput bold)\]\[$(tput setaf 2)\]\\$\[$(tput sgr0)\] "

Fail #3

I'm starting to give up by now, my last chance was using escape sequences directly like so:

PS1='\u @ \h $PWD \e[1;32m\$ \e[0m'

It garbles my terminal as described before, I'm currently setting it without colors to be able to work.

  • have you tried customizing your PS1 with http://bashrcgenerator.com/? It's a lot less time-consuming. I know this doesn't directly answer your question, but it might help. – Daniel Kelley Jul 02 '21 at 13:00
  • 1
    You don't want to have it in double quotes, you want single so that the various variable (e.g. $PWD or $(tput bold)) are not expanded when your bashrc is read but when the PS1 is being displayed. Go back to the single quoted one, the issue isn't caused by the quotes. – terdon Jul 02 '21 at 15:41

1 Answers1

1
PS1='${debian_chroot:+($debian_chroot)}\u @ \h $PWD \[$color_green\]\$\[$color_reset\] '

See this answer of mine. You need to wrap in \[ \] any non-printable sequence and nothing more. Fragments that print (i.e. add to the length of the prompt) should not be wrapped.

In your tries you either wrapped too little (like nothing) or too much ($PWD).