Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I’m creating custom dotfiles (.bash_profile and the like) for use with OS X’s and

I’d like to configure some settings conditionally, based on whether the active application supports 256 colors or not. ( supports it, but doesn’t.) This would be possible using an if statement (pseudo-code):

if 256_COLOR_SUPPORT; then
  # Make use of all available colors (
  # Fall back to 16-color mode (

The problem is I don’t know how to write the condition for the if statement.

Hence my question: is it possible to detect 256 color support? If so, how?

Update: I found out how to detect which terminal application I’m currently in:



So for now, I’m using the following check:

if [ $TERM_PROGRAM == "" ]; then
  # Groovy 256-color stuff

I’d still like to know if it’s possible to detect the number of available colors in the active application though. I tried tput colors but that seems useless since lies and returns 256.

share|improve this question
up vote 2 down vote accepted

That can be done via the TERM variable. Normally it would be set to "xterm", but in a 256-color terminal you can set it to "xterm-256color". The terminfo database has an appropriate entry for that, and many programs automatically switch to 256-color mode when they see "xterm-256color".

(Does really not have 256 color support?)

share|improve this answer
The problem is $TERM can be set to xterm-256color in as well, so this technique is not very robust. And no, really doesn’t support 256 colors, sadly. Try using 256 colors in and you’ll see stuff flickering all over your screen :( Maybe in OS X 10.7 Lion… – Mathias Bynens Mar 25 '11 at 12:15
The default values for $TERM seem to be these: xterm for, and xterm-color for I’m not sure how reliable this is, but I suppose that could be used to detect which terminal is active. I’m not sure if it would be better than using $TERM_PROGRAM though. Thanks for your answer! – Mathias Bynens Mar 25 '11 at 12:18
The TERM_PROGRAM variable appears to be specific to Mac terminals, but as long as that's all you need the dotfiles for, I guess that's fine. The TERM variable, meanwhile, is standard across systems and terminals, and setting it appropriately tells programs (via terminfo) what to expect from your terminal. – ak2 Mar 25 '11 at 13:38
Thanks for the info. – Mathias Bynens Mar 25 '11 at 13:59

I had the a bit of the same issue I think, and the way btw to force into 8 bit is to go to the:

  1. Preferences;
  2. Settings;
  3. Pick color scheme;
  4. Advanced tab.
  5. Then set "declare terminal as" to xterm-color instead of xterm-256color
share|improve this answer

tput colors returns the number of colous the current terminal type supports:

matt@dell:~$ tput colors

So you can capture that into a variable:

COLORS=$(tput colors)

and compare that variable with known / expected values

if [ $COLORS -eq 256 ]; then
   ... do your 256 colour stuff
   ... just 16 or 8 colours (16 is 8 colours in normal and bright mode)


Your update happened while I was writing that answer.

You should force to use an 8 colour terminal type (not sure how)

share|improve this answer
That depends on TERM being set appropriately in the first place. Otherwise, as long as TERM==xterm, tput colors will yield 8. – ak2 Mar 25 '11 at 11:52
Indeed, and so, it is not relevant. I never changed iTerm settings about colors support and when I echo TERM, it displays me xterm. So the information is not reliable concerning the functionality. Since then, is there another way to find out if term supports 256 colors? – Adrien Giboire Jan 13 '13 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .