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;32m"
blu="\033[0;34m"
def="\033[0;00m" # default
echo -e "Change to ${red} red to ${def} default to ${blu} blue."
# now store the current color (which happens to be blue) e.g.:
cur=????
echo -e "Change to ${grn} green and back to what I had before ${cur}"
echo -e "This would be in blue if variable cur contained e.g.: 0;34m."
echo -e "Back to default${def}"
exit 0
The answer that eludes me is how to capture the current color
cur=????
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 tostdout
andstderr
. (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
orkonsole
. 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 byxterm
and other graphical consoles. There's an list ofxterm
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.Notes
bash
uses a library calledreadline
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. InPS1
you need to surround console control sequences with\[
and\]
precisely becausereadline
does not know that they are control sequences.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.,
using the notation given in XTerm 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.