1

There is a strange behavior in all or most Linux terminal, which has bugged me for a long time. I am not sure if it is a bug or intended, or a configuration issue. So I ask here first.

Steps to reproduce

  • Open a terminal.
  • Optional: Run export PS1='~ \$', to standardize this setting. Feel free to play around with this setting.
  • Run echo -n hello.
  • Use "Up" key to get the previous command.
  • Use "Down" arrow key to get back to the empty prompt.
  • Use "Up" again.

Expected

(this is with my own $PS1 that leaves ~ $.)

After running echo -n hello:

~ $ echo -n hello
hello~ $ 

After "Up" arrow key:

~ $ echo -n hello
hello~ $ echo -n hello

After "Down" arrow key:

~ $ echo -n hello
hello~ $

After second "Up" arrow key:

~ $ echo -n hello
hello~ $ echo -n hello

Actual

(this is with my own $PS1 that leaves ~ $.) (first two steps as in "Expected")

After "Down" arrow key:

~ $ echo -n hello
hell

After second "Up" arrow key:

~ $ echo -n hello
hellecho -n hello

Variations

  • Use different values of $PS1.
  • Use strings of different lengths in echo.
  • Go further back in the history. For me, some history values have a different effect than others.

My terminal

I am using

Konsole Version 21.12.3

But I think I saw this also in other terminals in the past. I don't know if KDE "Konsole" is responsible for this behavior.

Additional information

(in response to comments below)

It does not happen for user "root"!

Initially I saw a difference between my own user and user root. But it turned out this was only due to differences in the $PS1 setting. After normalizing this value to '~ \$', or '${debian_chroot:+($debian_chroot)}\[\033[01;34m\]\w\[\033[00m\] \$ ', I now see same behavior for root and myself.

It also happens in bash inside a docker container which has a completely independent setup.

Links

For the record, I opened an issue here, https://bugs.kde.org/show_bug.cgi?id=464384

10
  • Can you reproduce this in another terminal? What if you log in as a different user? What if you try a different shell (sh or dash should be available). What shell are you using?
    – terdon
    Jan 16 at 17:13
  • Seems to be bash. If I use dash or sh, the up/down keys just produce ^[[A and ^[[B.
    – donquixote
    Jan 16 at 17:15
  • bash --version GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
    – donquixote
    Jan 16 at 17:16
  • OK and other terminals?
    – terdon
    Jan 16 at 17:16
  • echo $TERM -> xterm-256color. Now I am not sure what to look for and what to change.
    – donquixote
    Jan 16 at 17:18

1 Answer 1

1

That's just bash (actually readline) thinking that the prompt starts at the leftmost position of the current line, then when you press Down, just moving to position 5, and clearing to the end of line.

When you press Down, bash sends the following to the terminal:

\r\33[C\33[C\33[C\33[C\33[K

That's a carriage return to move to the leftmost position of the current line, four (the length of the prompt) CUF (Cursor Forward) control sequences, and an EL (Erase in Line) control sequence, which with parameter 0 (default) clears from the current cursor position to the end of the line.

2
  • Ok, this is good to know. Can anything be done about it?
    – donquixote
    Jan 17 at 20:23
  • 1
    @donquixote: not that I know of
    – ninjalj
    Jan 21 at 22:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .