2

Can I make bash automatically substitute one color code for another? For example, whenever a program outputs text wrapped in color code for bold foreground, I would like bash to actually use code for green background, or something.

The reason I want this is because my terminal emulator (Terminator) doesn't allow me to change the color of bold foreground - for example, echo -e "\e[1;39m Foo" will always output "Foo" in white, regardless of the color palette I set, which produces ugly results. I would like to make bash use \e[1;37m code (bold white, which I can change in Terminator preferences) instead of \e[1;39m.

Edit: from what I understand, doing what I want is more about things like termcap or terminfo than bash itself. Anyone experienced with these two?

11
  • 4
    That won't be trivially possible in a »bashish« way, as bash is the wrong program to ask for it. The shell does not interpret output of other programs. It's the terminal emulators task to do this. If your terminal emulator cannot do this for you, you should consider replacing it by some that actually will. What kind of b0rken emulator is this Terminator-thingy‽ ;) Mar 16, 2016 at 11:45
  • Or as a horrible kluge you'd need to do something like unix.stackexchange.com/questions/269002/… and then muck around with the control sequences as they fly by. Probably better to change the terminal.
    – thrig
    Mar 16, 2016 at 14:03
  • @AndreasWiese well, there are things like termcap and terminfo that are supposed to tell other programs what escape sequences to use. Thanks to unix.stackexchange.com/a/147/32950 I was able to do just what I want for less - and I was hoping I could do something similar for all other programs. Unfortunately I have trouble finding good examples showing how to use termcap... Mar 16, 2016 at 14:50
  • Problem I see here is that less actually interprets the content it displays, resp. it actually displays it. bash just executes commands; those commands are usually responsible for their output themselves. Mar 16, 2016 at 15:05
  • 1
    I'm afraid termcap/terminfo won't help you either, because that's not where colors go through. The normal solution would be to use a terminal emulator that supports configuring colors. There may be a way by running your program under an intermediate layer like screen or tmux but I don't think either of them support rewriting colors. Mar 16, 2016 at 22:19

1 Answer 1

1

Bash has no say about the colors that applications use, except of course what bash itself uses. Applications interact with the terminal; bash only starts them and notices when they finish.

Applications know how to talk with a terminal through the termcap (traditional) or terminfo (modeern) database. Termcap is older than colors, but terminfo does have some knowledge of colors. Look at the terminfo(5) man page for a list of supported capabilities on your system. There's no way to specify control sequences for individual colors, but you can achieve essentially the same result by configuring the appearance of each color through the initc capability. For example this changes the apperance of color 1 (normally red) to bright green:

tput initc 1 0 255 0

The ncurses database doesn't include this capability for terminator or for xterm though (as of version 5.9 on Debian jessie). However these terminal emulators (as well as any other terminal based on vte) do support an escape sequence which can be used for that purpose: OSC 4 ; c ; spec BEL. This is how to make red into bright green:

printf '\e]4;1;#00ff00\a'

The basic colors are numbered 0–7; bold text in those colors uses colors 8–15. You can also use OSC 5 ; 0 ; spec BEL to change the color used for bold in the default color, and so on (see the control sequence list for details).

These settings affect both foreground and backgrouns colors, there's no way to affect foreground and background separately.

To customize the appearance of colors, you would output these control sequences from your .bashrc. That would only affect terminals where you run an interactive instance of bash, not terminals where you start another application directly.

I don't know how this interacts with Terminator's color palette settings.

5
  • That doesn't answer OP's question (although OP may suppose so). Mar 19, 2016 at 17:31
  • @ThomasDickey While it doesn't do exactly what the question asks, it's as close as it gets, isn't it? It isn't clear to me whether that's enough to solve the underlying problem (I don't understand exactly how much control over the attribute combinations is required). Mar 19, 2016 at 17:55
  • @Gilles hmm. printf '\e]4;1;#00ff00\a' does change my red to green (although it changes back as soon as the terminal window looses focus), but printf '\e]5;0;#00ff00\a' (nor any other combination I tried) doesn't affect foreground color (which is not the same as "white" color). Mar 19, 2016 at 22:27
  • @Gilles I have tried printf '\e]5;0;#00ff00\a' in terminator, gnome-terminal, konsole, urxvt and xterm, and none worked :-( (while printf '\e]4;1;#00ff00\a' worked everywhere except Konsole). Am I doing something wrong or should we change the answer to say that changing bold fg color is impossible? I'd like to accept it. Mar 23, 2016 at 11:48
  • @JanekWarchoł Oh. With xterm, you need to run it with the resource XTerm.vt100.colorBDMode: 0 or the command line option +bdc if you want to use color instead of bold. If you want both bold and color, use the resource XTerm.vt100.veryBoldColors: 255 as well. I don't know if vte has something analogous. Mar 23, 2016 at 12:45

You must log in to answer this question.

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