The meat

In (some) terminals you can use a sequence like this;

printf "\e]4;3;rgb:cc/78/33\e\\"

to set the third color in the ANSI color palette to the RGB values represented by #CC7833. Can you do the opposite? Can I get the current RGB value (in any form) of the third (well, any really) color?


The reason you'd want to do this, in my case, is to be able to save the entire color table before messing it up (on purpose). I want to be able to list code in the terminal using an RGB exact color theme, call me anal. To do that I have to override the color definitions in the terminal color table, as per above.

For me the problem is now solved. But since I want to add this functionality to an existing open source project (pry) used by a lot of people I need to be able to save the current state of the color palette before setting the theme colors for the programs session and then switch them back after.

I know some terminals have this in settings and some systems have it in config files. But I need something that is general and would work across systems. Ideally all the systems that support setting them in the way above :)


Right now the changing of colors affect the entire system, all terminal sessions, even after closing and restarting the terminal (this is using iTerm2 on OS X Mountain Lion).

If the effect could be made local, say to a sub shell, the problem would go away since the changes would die with the sub shell when the process ended. Even better since it would protect against cases where the session crashed...

3 Answers 3


You can query the color with

printf "\e]4;3;?\e\\"

which places the response in the keyboard buffer as if you've typed it in the terminal (which is hard to process in shell scripts). Or you can use

xtermcontrol --get-color3

which a convenience frontend using this escape sequence.

  • Note that not every terminal reacts to this sequence. For example, st doesn't output anything. Also, any two terminal's output for this can (and probably will) differ.
    – domsson
    May 26, 2019 at 18:05

The closest to a standard method for this that I can think of would be to rely on xrdb. You could use xrdb -query and get the color values used by the current X session. For example, mine provides the following:

*background:    #151515
*color0:    #101010
*color1:    #E84F4F
*color10:   #A0CF5D
*color11:   #F39D21
*color12:   #4E9FB1
*color13:   #8542FF
*color14:   #42717B
*color15:   #dddddd
*color2:    #B8D68C
*color3:    #E1AA5D
*color4:    #7DC1CF
*color5:    #9B64FB
*color6:    #6D878D
*color7:    #dddddd
*color8:    #404040
*color9:    #D23D3D
*foreground:    #D7D0C7

Unfortunately, due to the way xrdb works, I'm not aware of any way to only set settings for a sub-shell, as the entire point of xrdb is to coordinate settings for the current x-session. However, you could "hack" around it by exporting the settings via xrdb -query and then reloading them when you are done.

  • This is a viable option only on systems that use X11 or the same set of tools. It won't work on OS X or other Linux display servers such as Wayland or Mir ... Oct 15, 2013 at 14:00
  • Ah, yes. I'm sorry -- I assumed OS X used X for some reason.
    – tgood
    Oct 15, 2013 at 18:19

The classic way I used was to define a set of xrdb properties for a new class --- like that: (in linux you have to put it in $HOME/.Xdefault-hostname, YMMV)

MYXTerm*foreground: red;
MYXTerm*color11: navyblue;

and if necessary (not sure, depends on the system) loading the file using xrdb -merge. After that, starting a terminal with:

xterm -class MYXTerm

Now you should have a terminal with a set of private colors that do not affect the normal ones . Now, if what you want is to run your program into the same terminal, I really do not know how to do it...

  • This is a viable option only on systems that use X11 or the same set of tools. It won't work on OS X or other Linux display servers such as Wayland or Mir ... Oct 15, 2013 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.