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:
- Host sends: DCS
m ST (DECRQSS with the control function part of SGR as the setting)
- Terminal responds: DCS
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
rc. The terminfo entries are
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