I would like to query and store the current terminal color pair in BASH e.g.:
#!/bin/bash
#some ANSI colour escape sequences
red=\"\\033[0;31m\"
grn=\"\\033[0;3
It's important to understand that the terminal state has nothing to do with bash. Bash doesn't care. It simply reads from stdin
and writes to stdout
and stderr
. (See Note 1)
All terminal effects are implemented by the terminal emulator you happen to be using, of which there are many. In a graphical environment, you might be using, for example, xterm
or konsole
. You'll need to search the documentation for those emulators for specific terminal control codes which they interpret.
As far as I know, there is no standard code to get a report of the current terminal state, other than the cursor position (ESC[6n).
So your best bet is to remember the changes you made when you make them.
You can find a list of the standard codes implemented by the Linux console using man console_codes
(although few people use the Linux console these days); most of those are also interpreted by xterm
and other graphical consoles. There's an list of xterm
sequences in Thomas Dickey's xterm site; it's a more or less de facto standard for terminal emulators but, as I said, you'll need to search in each emulator's documentation for idiosyncratic control sequences.
bash
uses a library called readline
to help it handle some terminal effects. In particular, readline
tries to maintain the current console cursor position, although it is easy to fool it. In PS1
you need to surround console control sequences with \[
and \]
precisely because readline
does not know that they are control sequences.You can't; there is no standard control sequence to report the current cursor attributes.
What does exist, however, is a sequence to save and restore the current cursor position and attributes:
\e7
(DECSC) will save the cursor position and attributes.\e8
(DECRC) will restore the saved cursor position and attributes.There is no standard way to restore only the cursor attributes; however, as rici mentioned, you can get a report of the current position using \e[6n
(DSR), then use the response to manually "un-restore" the cursor position after restoring its position and attributes.
Again, though, it's probably easier (and better) to just keep track of the colors in your application, rather than making the terminal responsible for that.
The question was about the current color, not the cursor position.
Both are "nonstandard" (though the latter, cursor position report is implemented by anything which has a valid claim to "VT100 emulator").
However, xterm implements a set of escape sequences referred to as dynamic colors, which predate the ANSI color functionality. Those set the working colors including text foreground and background. I modified this in 2002 to allow an application to send the sequence with a "?" rather than a color to tell xterm to return the color value, e.g.,
OSC 1 1 ? ST
using the notation given in XTerm Control Sequences