command-line – Background Color Whitespace When Terminal Reaches End

colorscommand lineescape-charactersterminal

I am working with control sequences for text formatting and have stumbled upon some unexpected behavior. I have some text to output, in the middle of this text there is some smaller text block highlighted with a colored background. The original text may be pretty long, it may take a few lines, so the colored text block in the middle may appear on the multiple lines – starts on the one line and finishes on the other. Everything seems to work fine until it reaches the bottom of the terminal window, a lot of whitespace becomes colored:

enter image description here

Here is a script to reproduce:

# color.sh
echo -e 'default \x1B[43m color \n color \x1B[49m default';

As you can see, I've added a newline character \n in the text block, just to reproduce, but it my situation, when a text is pretty long and it does not fit into a single line, colored block gets split into multiple line, and as a result I have the same colored whitespace after the text.

# color-long.sh
echo -e 'default default default default default default default default default \x1B[43m color color \x1B[49m default';

enter image description here

I get this on Ubuntu 14.04, but was able to reproduce the same behavior on Yosemite 10.10.

I wonder what is the reason of this behavior and how it can be fixed without using different utilities for output (instead of echo), but maybe by using the same control sequences. I have control over the text, but not over the output process.

I've already tried to wrap sequences like \[\x1B[43m\], \001\x1B[43m\002, but it does not give any result, just adds extra [] to the text or outputs some unrecognized symbols.

Best Answer

This has nothing to do with bash, it is purely an effect of the terminal's behavior, specifically scroll. When you reach the bottom of the screen, and start to type on the next line, the terminal creates a new blank line by pushing everything up one line. (In older terminals this destroys the top line. In newer terminals the top line is just pushed into the scrollback buffer.) Now here is the hard question, what color is the new line. The foreground color is not an issue, because you cannot see it, but the background color . . . (In the days they started arguing it could be black gray or white (or actually black green, bright green or the same in amber)) There were experiments to just use black, but there were complaints. What was settled on is that the new line (or cleared area in a clear up the whole screen in the case of a screen clear) would have the current background color as the background color of the affected area. So this behavior is by design, because it does the right thing in most cases.

So what do you want to do to get the behavior you want? When you change back to black background (or at the end of the line) send a clear to end of line which will set the background color for the rest of the line.