Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI escape code weird behavior at end of line

Steps to reproduce

Consider the following shell command:

echo -e "\e[41mTest\nTest2\e[0mTest3"

It prints Test and in the next line Test2 with a red background (using an ANSI escape code). Test2 is followed directly by Test3 which is uncolored.

Behaviour

The first time this command is executed everything works as expected. However the output is not consistent. After about 10-20 invocations the end of the second line turns red as well. I first stumbled upon this in my C++ App. So I thought it's probably not related to bash.

After the discovery I figured it may be a bug in gnome-terminal. However the behavior is exactly the same in xterm.

Edit

Thanks to the comment of Geno Chen I figured out that this occurs when the terminal runs out of lines and has to scroll.

Screenshots

Here are the screenshots of aforementioned problem:

GNOME Terminal

GNOME Terminal

xterm

xterm

Things in question

Although it makes things a bit unclear in my C++ App it is not a deal breaker. However I'd like to know if there is something wrong with my escape sequences or if this is a bug in some part of the shell. And if there is something I can do to fix it or workaround it.

like image 279
Scindix Avatar asked Dec 12 '18 10:12

Scindix


1 Answers

Filling the line with the currently-selected colors is a detail of bce (back color erase) which could be implemented differently in different terminals—but Linux console and xterm happen to do it this way. It's an FAQ:

  • My terminal shows some uncolored spaces (ncurses, see indn)
  • That color scheme is odd, say more? (xterm)
  • Who did it? (some comments on the origin of this particular design)
like image 144
Thomas Dickey Avatar answered Sep 27 '22 22:09

Thomas Dickey