1

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

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

if 256_COLOR_SUPPORT; then
  # Make use of all available colors (iTerm.app)
else
  # Fall back to 16-color mode (Terminal.app)
fi

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:

iTerm.app:

$ echo $TERM_PROGRAM
iTerm.app

Terminal.app:

$ echo $TERM_PROGRAM
Apple_Terminal

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

if [ $TERM_PROGRAM == "iTerm.app" ]; then
  # Groovy 256-color stuff
fi

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 Terminal.app lies and returns 256.

3 Answers 3

2

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 Terminal.app really not have 256 color support?)

3
  • 1
    The problem is $TERM can be set to xterm-256color in Terminal.app as well, so this technique is not very robust. And no, Terminal.app really doesn’t support 256 colors, sadly. Try using 256 colors in Terminal.app and you’ll see stuff flickering all over your screen :( Maybe in OS X 10.7 Lion… Commented Mar 25, 2011 at 12:15
  • The default values for $TERM seem to be these: xterm for iTerm.app, and xterm-color for Terminal.app. 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! Commented Mar 25, 2011 at 12:18
  • 1
    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
    Commented Mar 25, 2011 at 13:38
1

I had the a bit of the same issue I think, and the way btw to force terminal.app 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
0

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

matt@dell:~$ tput colors
8

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
else
   ... just 16 or 8 colours (16 is 8 colours in normal and bright mode)
fi

Update

Your update happened while I was writing that answer.

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

2
  • 1
    That depends on TERM being set appropriately in the first place. Otherwise, as long as TERM==xterm, tput colors will yield 8.
    – ak2
    Commented Mar 25, 2011 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
    Commented Jan 13, 2013 at 15:35

You must log in to answer this question.

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