9

I have a problem with colors in my terminal emulator. I am using LXTerminal as my terminal emulator and LXDE as my desktop environment.

The following command is supposed to print red text on grey background:

printf "\n\033[1;31;40m"hello"\033[00m\n\n"

but that does not work. The grey background is missing (red text is OK). On my older machine (Gnome/Gnome Terminal) everything works fine

I have tried installing several other terminal emulators, but the problem persists. I am not even sure whether this issue is related to terminal emulator, or something else. In Console (CTRL+ALT+F1) the background grey color does not work on either machine)

This problem is limited to grey background only. All other colors work OK, for instance the following prints red text on green background.

printf "\n\033[1;31;42m"hello"\033[00m\n\n"

I would appreciate any help. I don't even know where to start troubleshooting.

6
  • What does echo $TERM print?
    – terdon
    Commented Oct 6, 2013 at 16:38
  • @terdon It won't make any difference.
    – goldilocks
    Commented Oct 6, 2013 at 16:38
  • @goldilocks isn't the $TERM read to determine how escape sequences should be handled?
    – terdon
    Commented Oct 6, 2013 at 16:40
  • 2
    @terdon: no, $TERM is read by (n)curses/slang/...-using programs to determine which escape sequences to send.
    – ninjalj
    Commented Oct 6, 2013 at 21:08
  • 1
    @goldilocks: konsole can do it via TERM=konsole-256color tput setab 8; echo foobar; tput setab 0
    – ninjalj
    Commented Oct 6, 2013 at 21:16

4 Answers 4

13

There are eight standard ANSI colors, supported by every terminal emulator. Most terminal emulators also have eight bright variants of the standard ANSI colors.

However, the actual color values that the escape codes map to aren't standardized, and in fact they often slightly vary among terminal emulators. So if you do printf "\e[31;47mTest\n" to print red text on a white background, the actual hues of red and white you get may be different depending on the terminal emulator you use.

So that partly explains the problem: color values aren't standard, and LXTerminal may have different defaults for its color palette that you're not used to. If you look around in the settings, usually you can configure the color scheme to be whatever you like.

The other problem you face is that what the bold attribute actually does isn't standardized either. There are three possibilities: it can make the font bold, it can make the foreground color brighter, or it can both make the foreground color brighter and make the font bold.

Again, the default behavior here varies among terminal emulators, and you can usually change it if can you find the right setting. Grep for something mentioning 'bold' or 'bright'.

If you want to use a bright color, then you can use the so-called aixterm color escape codes instead of bold. These aren't standard, but they're supported in every modern terminal emulator I know of. Unlike bold, they always use bright colors, plus they can be used to display bright background colors.

So for example, if you wanted to print bright red text on a bright white background, you would do this: printf "\e[91;107mTest\n".

For reference, here's a table of all the color escape codes:

|    ANSI |    ANSI |    ANSI    |                | Aixterm | Aixterm 
|   Color | FG Code | BG Code    | Bright Color   | FG Code | BG Code 
+---------+---------+--------    +----------------+---------+-------- 
|   Black |      30 |      40    |   Bright Black |      90 |     100 
|     Red |      31 |      41    |     Bright Red |      91 |     101 
|   Green |      32 |      42    |   Bright Green |      92 |     102 
|  Yellow |      33 |      43    |  Bright Yellow |      93 |     103 
|    Blue |      34 |      44    |    Bright Blue |      94 |     104 
| Magenta |      35 |      45    | Bright Magenta |      95 |     105 
|    Cyan |      36 |      46    |    Bright Cyan |      96 |     106 
|   White |      37 |      47    |   Bright White |      97 |     107 
6
  • actually, the problem is not limited to these "escape codes". I have the same problem with joe editor. In gnome-terminal, I can see grey background. In LXTerminal it is missing. Commented Oct 7, 2013 at 10:35
  • And you are right about the actual colors varying between different terminal emulators. When I open midnight commander in LXTerminal and gnome-terminal, the blue color looks very different. But back to my original problem: the grey background in LXTerminal is not just different color than in gnome-terminal, but it is missing entirely. Commented Oct 7, 2013 at 10:49
  • 1
    What do you mean that the grey background is missing? It's probably there, but not visible because it's the same color as your background. Technically there is no grey color in ANSI, there is only black or white. So maybe you have your background color set to black, and ANSI black is mapped to black in LXTerminal but mapped to grey in gnome-terminal. I don't use LXTerminal, but I bet if you look in the settings and change any 'black' colors to 'grey' it will become visible.
    – Matt
    Commented Oct 7, 2013 at 18:25
  • You are absolutely right. When I change the background color of my LXTerminal to something other than black, than I can see that printf "\n\033[1;31;40m"hello"\033[00m\n\n" prints text on black backround. Thus, what gnome-terminal interprets as grey, LXTerminal interprets as black. Is there any way to display grey in LXTerminal, same as in gnome-terminal ? Commented Oct 8, 2013 at 7:52
  • 2
    Actually aixterm is SGR 90-97 and 100-107. Bright backgrounds for bold are from other systems (not original to Linux, but used there). Commented Jul 29, 2016 at 1:06
5

The 1 attribute at the beginning of 1;31;40 turns on "bold" (aka. "bright") mode, but some terminals only apply this to the foreground color. I have no idea for what reason (and changing the $TERM variable makes no difference), since they do support 16 colors in the foreground.1 0 (from 40: 4 = background, 0 = the color) is actually black, but "bright black" is gray. Here's two solutions:

  • Color 7 (white) will probably turn out to be gray, not white (for true white, you need "bright white", meaning the background you actually can't have is white).

  • You should be able use gnome terminal in LXDE if you want. Just install it and start with gnome-terminal (I believe that's its command name). Then you'll have to configure whatever launchers to use that instead of the lx-terminal.

1 Here's my guess: Notice that setting bold applies brightness to the foreground color and adds a bold emphasis. Hence, you can't use a bright foreground color without also applying bold. Presumably back when monochrome terminals were common, just plain setting bold for emphasis, eg: \\033[1mBlah blah\\033[0m was common -- and in fact this will still work, but it will also make your foreground color bright. If we considered it undesirable for this to also brighten the background (because it is just supposed to be bold), then we'd make sure the implementation didn't do that. Oh well.

2
  • I have installed gnome-terminal and the colors are displayed correctly. Does it mean this problem is specific to LXTerminal ? Is there any way to fix this ? I would prefer to use LXTerminal, if this problem could be solved. Commented Oct 6, 2013 at 19:09
  • 3
    The problem isn't specific to LXTerminal; in fact gnome-terminal maybe the only terminal to do what you want -- I've been a linux user for ~15 years and learnt about ANSI sequences early on because of the prompt; it's always been like this. I might be able to figure out decisively why by researching the official ANSI specs and/or wading through source, but that would require a very indeterminate period of time ;) I haven't found anything hopeful after browsing the web, so yours is a good question but someone who already knows could well come along in the next day or so...
    – goldilocks
    Commented Oct 6, 2013 at 19:26
2

Here's a trick you can use with some terminal emulators. It will give you bright background colours by using the reverse-video attribute (7) to select a bright foreground colour, and then swap it with the background colour.

echo -e "\e[1;32;7mText on a bright green background"

The catch is that although you can now have 16 background colours, the limitation of 8 colours now applies to the foreground/text instead. And to further confuse matters, while the reverse-video attribute is active, you have to set the text colour by using the attribute codes for background colours!

1
  • 1
    the reverse method is the only method that works in every terminal I tested: win xp,win7 (mintty, cmd cygwin, conemu cygwin), debian (xfce, xterm) echo -e "\e[1;33;40;7m TEST2 \e[0m" Commented Apr 21, 2020 at 10:27
0

With 47=background white instead of 40=background black you get the non-bold white as background.

]# printf "\033[0;30;47m"hello"\033[00m\n"
hello (black on grey)
]# printf "\033[1;30;47m"hello"\033[00m\n"
hello (darkgrey on grey)
]# printf "\033[1;37;47m"hello"\033[00m\n"
hello (white on grey)
]# printf "\033[0;37;47m"hello"\033[00m\n"
hello (INVISIBLE grey on grey)

Bold black results in dark grey, by default. Normal white is actually gray, because bold white is white.


This loop prints two columns with all color-on-black and white-on-color combinations.

for ((i=30; i<=47; i++))
do
    echo -en  "\e[${i}mHello\e[0m";
    echo -e "\e[1;${i}mHello\e[0m";
done

It starts with invisible black and darkgrey-on-black, but the other 17 lines look nice and it shows the whole console color palette.

(38 and 39 should be left out, but for simplicity I just run from 30 to 47)


Acceptd A. mentions eight colors and lack of standards.

The six colors 31-36 (red to cyan) are well defined. Black (30) and White (37) also, but their bold variants less, especially as background (40 and 47).


I just notice: in xterm the normal white text is visible on grey background, but in linux console not. (last line, with i=47) (until 30 min ago the script stopped at 46, because I wanted no unreadable variants)

In both places, when mouse-selected, it adapts cleverly and stays readable when reversed.

You must log in to answer this question.

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