Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I learned that I can change the format of PS1, especially the color of the string. Then, is it possible to change the color based on the background color of the shell? Or, how can I detect the background color of the shell?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

As far as I know, there's no way to query the colors of the terminal emulator. You can change them with \e]4;NUMBER;#RRGGBB\a (where NUMBER is the terminal color number (0–7 for light colors, 8–15 for bright colors) and #RRGGBB is a hexadecimal RGB color value) if your terminal supports that sequence (reference: ctlseqs).

Powerful color scheme mechanisms often have a dark or light background setting that you must supply to indicate whether you have a black or dark gray background, or a white or light gray background.

When you're configuring for yourself, it's usually enough to decide you'll always use the same background color.

share|improve this answer
You can get the supported number of colors in the termcap entry by looking at "tput colors" but in many cases that will say a low number, like 8, because the termcap support for 256-color terminals is really weak. – slacy Jun 12 '12 at 21:46
@slacy For >99% of today's terminals, termcap/terminfo data is irrelevant: they're all minor variations of xterm, and the details that differ, such as the number of colors, are not reflected in the value of TERM and so cannot be obtained from the environment. – Gilles Jun 12 '12 at 21:56

If you are using gnome-terminal you can get the background color for any profile, in this case for the Default profile as

   $ gconftool-2 --get /apps/gnome-terminal/profiles/Default/background_color

then you can decide how to set your prompt accordingly.

share|improve this answer

With xterm, you can query the background color with the \e]11;?\a escape sequence. You need to read the response from the terminal. There is a xtermcontrol command that does that for you:

~$ xtermcontrol --get-bg
share|improve this answer
The same works in gnome-terminal and probably some other terminal emulators too (but definitely not all of them). – egmont Apr 22 at 11:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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