ANSI escape code weird behavior at end of line ANSI escape code weird behavior at end of line shell shell

ANSI escape code weird behavior at end of line


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:


This is for everyone who searches a quick answer.From the links of Thomas Dickey's answer I came up with the following workaround.

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

The \e[K part paints the rest of the line with the current background color. As it needs to be sent before every newline character for every line that uses colors I have to rewrite my code a bit though...


@Scindix's answer worked great for me, so in order to fix the background colour wrapping, I wrote a tiny script, ./fill-background-colour-to-line-end, that I can pipe another command to:

#!/usr/bin/env ruby# frozen_string_literal: trueSTDIN.each_char do |char|  print "\e[K" if char == "\n"  print charend

Running OP's example and piping it to this, showing it's fixed

Not only does it fix the wrapping weirdness, but filling the background colour to the end of the line was what I'd been wanting anyway =)

Side note: I tried Tilix and LX Terminal; the behaviour there is the same.