Bash 5.2.15 (Debian 12) will produce erroneous output when PS1 contains
escape sequences. It will also misplace the cursor and corrupt the state
of the current line.
To replicate this issue:
1. Modify PS1 to contain one or more ANSI escape sequences, for example
"\e[45m" (tput setab 5, changes the background color to purple) or
"\e(B\e[m" (tput sgr0, reset for xterm terminals):
$ PS1="x$(printf '\e(B\e[m') \\\$ "
2. Press the up arrow to recall the last command. The command line will
now show [■ indicates the position of the cursor]:
x $ PS1="x$(printf '\e(B\e[m') \\\$ "■
3. Press the down arrow to go back to the empty prompt.
What one should see at this point is just the prompt
x $ ■
Instead, left-overs from the recalled line will be visible and the
cursor will be placed in the wrong spot:
x $ PS1="x■
The amount of garbage left on the prompt is proportional to the number
of escape sequences:
PS1="x$(printf '\e(B\e[m')\\\$ " => 6 left-over chars
PS1="x$(printf '\e(B\e[m%.0s' {1..2})\\\$ " => 12 left-over chars
PS1="x$(printf '\e(B\e[m%.0s' {1..4})\\\$ " => 24 left-over chars
Tested with TERM = linux, xterm, xterm-256color.
Regards,
(This bug has also been reported at <https://bugs.debian.org/1051388>.)
--
Gioele Barabucci