#1018851 bash: long PS1 may or may not cause bad cursor positioning, depending on the position of a space

Package:
bash
Source:
bash
Description:
GNU Bourne Again SHell
Submitter:
Michael Gold
Date:
2024-08-08 20:51:02 UTC
Severity:
normal
Tags:
#1018851#5
Date:
2022-09-01 01:10:52 UTC
From:
To:
Dear Maintainer,

I was seeing incorrect cursor behaviour when my PS1 prompt got long, and
was surprised to learn that merely moving a space character from after a
control sequence to before it fixed the problem.  Based on the man page,
I think I've used the "non-printing" escapes correctly.

Try this in an 80-character-wide terminal:
x159=''
while [ "${#x159}" -lt 159 ]; do x159=x$x159; done
PS1=$'\\[\x1b[0;33;40m\\]'$x159$'$\\[\x1b[0m\\] '

Then enter an ordinary character such as 't'.  I see my cursor move up a
line to the final 'x' before '$', instead of the expected position after
the 't' I just entered.  But I see no problem with this prompt:
PS1=$'\\[\x1b[0;33;40m\\]'$x159$'$ \\[\x1b[0m\\]'

I only moved the trailing space.  It's outside the non-printing sequence
delimited by \[ and \] in both cases, so I'd expect no difference except
the colour of that cell.

- Michael

#1018851#10
Date:
2024-08-07 20:47:30 UTC
From:
To:
Version: 5.2.21-2.1

Hi,

this issue does not seem to be reproducible in version 5.2.21-2 of bash.

Please reopen this bug if you can still reproduce this issue.

Regards,

#1018851#15
Date:
2024-08-08 16:42:57 UTC
From:
To:
reopen 1018851
stop

Thanks for looking at this.

I'm using bash package version "5.2.21-2.1".  Upon further investigation
under variously "clean" environments, I can still re-produce this inside
urxvt (rxvt-unicode 9.31-3+b1), but only if LANG is set.

I used the bwrap (bubblewrap) utility to hide all user directories, and
Xepher to ensure a clean X11 environment (xrdb -query shows nothing).
I ran "urxvt -cr red -e /bin/bash", setting the cursor colour to red to
make it more visible when the problem occurs.

I'm attaching screen-shots.  term1.png is immediately after setting the
prompt, before typing "t"; term2.png is after typing "t"; term3.png is
after further typing "est" (that is, typing "test" without pressing
Enter).  By using the left and right arrow keys, I can then cause the
cursor to "walk" up the terminal, as shown in term4.png.

If I leave LANG unset, the problem does not occur.  Its normal value is
en_CA.UTF-8 on my system, and C.UTF-8 shows the same behaviour.

#1018851#24
Date:
2024-08-08 19:23:00 UTC
From:
To:
Control: tags -1 confirmed
Control: found -1 5.2.21-2.1

Oh, I see. In my tests I had LC_* set, but LANG was not set.

I can confirm that this issue happens also with gnome-terminal and
xterm, as long as LANG is set to an available locale ending with ".UTF-8".

To reproduce:

     $ set LANG=C.UTF-8
     $ x=$(eval "printf x%.0s {1..$((COLUMNS*2-1))}")
     $ PS1=$'\[\e[0;34;42m\]'$x$'\[\e[0m\]\$ '
     (press up arrow)
     (type anything)

Regards,