How to determine the current color of the console output?

In an xterm you can get the current color RGB color codes reported by altering a color change escape to a query. Use the ESC ] Ps m - but add a ? question mark. From the docs:

  • If a "?" is given rather than a name or RGB specification, xterm replies with a control sequence of the same form which can be used to set the corresponding dynamic color. Because more than one pair of color number and specification can be given in one control sequence, xterm can make more than one reply.
    • P s = 1 0 → Change VT100 text foreground color to P t .
    • P s = 1 1 → Change VT100 text background color to P t .
    • P s = 1 2 → Change text cursor color to P t .
    • P s = 1 3 → Change mouse foreground color to P t .
    • P s = 1 4 → Change mouse background color to P t .
    • P s = 1 5 → Change Tektronix foreground color to P t .
    • P s = 1 6 → Change Tektronix background color to P t .
    • P s = 1 7 → Change highlight background color to P t .
    • P s = 1 8 → Change Tektronix cursor color to P t .
    • P s = 1 9 → Change highlight foreground color to P t .

I have serious doubts if this is likely to work in another terminal emulator, but in an xterm if you run...

printf '\033]11;?\007'

...the xterm will push back into your terminal's input buffer a sequence like the following...

11;rgb:ffff/ffff/ffff

...for the background or for the foreground:

printf '\033]10;?\007'

10;rgb:0000/0000/0000

In general, obtaining the current colours is impossible. The control sequence processing of a terminal happens "inside" the terminal, wherever that happens to be. With a terminal emulator such as xterm or the one built into an operating system kernel that provides the kernel virtual terminals, the internal state of the emulator, including its notion of the current "graphic rendition" (i.e. colour and attributes), is on the machine itself and is theoretically accessible. But for a real terminal this information is in some RAM location on a physically separate machine connected via a serial link.

That said, some terminals include a mechanism for reading out such information as part of their terminal protocol, that is sent over that serial link. They provide control sequences that a program can send to the terminal, that cause it to send back information about its internal state, as terminal input. mikeserv has shown you the control sequences that the xterm terminal emulator responds to. But these are specific to xterm. The built-in terminal emulators in the Linux kernel and the various BSD kernels are different terminal types, for example, and don't implement any such control sequences at all. The same goes for whole families of real terminals.

DEC VT525 terminals implement a read-out mechanism, but have a set of control sequences that bears no relationship to those used by xterm. One sends the DECRQSS (Request Selection or Setting) sequence to request the current graphic rendition, and the terminal responds by sending the DECRPSS (Report Selection or Setting). Specifically:

  1. Host sends: DCS $ q m ST (DECRQSS with the control function part of SGR as the setting)
  2. Terminal responds: DCS 0 $ r 0 ; 3 3 ; 4 4 m ST (DECRPSS with the parameters and control function part of an SGR control sequence that sets the current foreground and background colours)

Of course, a careful reading of your question reveals that you are waving a chocolate-covered banana at those European currency systems again. What you're actually trying to do, for which you've selected a solution and then asked how to do part of that solution, is preserve the previous state whilst you write some colourized output. Not only is there a DEC VT control sequence for doing this, there's a SCO console terminal sequence for it that is recognized by xterm and various kernel built-in terminal emulators, and a termcap/terminfo entry that tells you what they are for your terminal.

The termcap entries are sc and rc. The terminfo entries are save_cursor and restore_cursor. The names are somewhat misleading as to effect (although they do act as a warning that you are relying upon something that is de facto rather than de jure). The actual DECSC, DECRC, SCOSC, and SCORC control sequences save and restore the current graphic rendition as well.

Given that the article that you pointed to is all about generating control sequences from shell scripts, the command that you are now looking for is tput.

Further reading

  • Jonathan de Boyne Pollard. 2007. Put down the chocolate-covered banana and step away from the European currency systems.. Frequently Given Answers.
  • VT420 Programmer Reference Manual. EK-VT420-RM-002. February 1992. Digital.
  • VT520/VT525 Video Terminal Programmer Information. EK-VT520-RM. July 1994. Digital.