30

Is there a reliable way to check how many colors my terminal emulator supports?

If echo $TERM prints xterm, does that unequivocally tell me how many colors my terminal emulator supports? How could I check this information reliably?

3 Answers3

26

The value of $TERM does not give much information about the number of supported colors. Many terminals advertise themselves as xterm, and might support any number of colors (2, 8, 16, 88 and 256 are common values).

You can query the value of each color with the OSC 4 ; c ; ? BEL control sequence. If the color number c is supported, and if the terminal understands this control sequence, the terminal will answer back with the value of the color. If the color number is not supported or if the terminal doesn't understand this control sequence, the terminal answers nothing. Here's a bash/zsh snippet to query whether color 42 is supported (redirect to/from the terminal if necessary):

printf '\e]4;%d;?\a' 42
if read -d $'\a' -s -t 1; then … # color 42 is supported

Among popular terminals, xterm and terminals based on the VTE library (Gnome-terminal, Terminator, Xfce4-terminal, …) support this control sequence; rxvt, konsole, screen and tmux don't.

I don't know of a more direct way.

  • 2
    Thanks @Gilles. This looks great. May I ask what read -d $'\a' -s -t 1 does? – Amelio Vazquez-Reina Nov 01 '11 at 22:50
  • 2
    @intrpc Read input until the first \a (bell character), without echoing input, with a timeout of 1 second. – Gilles 'SO- stop being evil' Nov 01 '11 at 22:55
  • The result on my terminal (color 42 is supported) is inconsistent with tput colors, which returned 8. What gives? – l0b0 Nov 02 '11 at 08:44
  • 7
    @l0b0 tput colors queries the terminfo database. Chances are that you have TERM=xterm. Xterm can support at least 2, 8, 16, 88 or 256 colors depending on the version and on compile- and run-time options, but the terminfo database can only store one value. You can set e.g. TERM=xterm+256color, but then you'll be annoyed when you log in to a machine that doesn't have this entry in its termcap/terminfo database. – Gilles 'SO- stop being evil' Nov 02 '11 at 11:59
  • @Gilles: Is it possible to read this non-interactively from the terminal? I've tried a printf | while read, but it just gets the characters from printf, not what will actually be printed by the terminal. – l0b0 Feb 24 '12 at 14:09
  • 2
    @l0b0 Your pipe obviously has nothing to do with the terminal. You need to print to the terminal (printf … >/dev/tty) and then read from the terminal (read … </dev/tty). Xterm responds to the OSC 4; …; ? BEL sequence by injecting keystrokes. – Gilles 'SO- stop being evil' Feb 24 '12 at 15:01
  • 6
  • @l0b0: In Konsole it shows 0 whereas actually it supports 256 colors... – jamadagni Oct 08 '15 at 14:56
  • @jamadagni Unfortunately Konsole doesn't support the control sequence we use. Thanks, I added a mention of that in my answer. l0b0: it might be useful to add a sanity check in your script: if the terminal doesn't even seem to support color number 1, the information is clearly not reliable. – Gilles 'SO- stop being evil' Oct 08 '15 at 15:22
  • @Gilles: Is there then any way to find out the correct number of colors on Konsole/Yakuake? Or should I just assume that since I'm running it under a full-color X session all 256 will be available? Is then there any way to find that the current terminal is controlled by Konsole/Yakuake? – jamadagni Oct 09 '15 at 01:49
  • @jamadagni Thanks - I've added a bug report and I'd be happy to merge a pull request. – l0b0 Oct 09 '15 at 18:11
  • @l0b0: Thanks for taking feedback into account. I have no pull request to make simply because I don't know how to fix it. I'm hoping Gilles or another such expert can chime in. From this page it would seem that the only reliable way is to get terminal emulators to set the TERM variable correctly with a -256color suffix... :-( – jamadagni Oct 11 '15 at 01:36
22

You can use

$ tput colors

On my debian install tput is part of the ncurses-bin package which is installed by default.

  • 8
    That will only inform how many colors your terminal is reporting to the environment via TERM, not how many colors it can actually support given an appropriate TERM – MestreLion Feb 03 '15 at 23:31
13

There is a perl script, 256colors2.pl, that will display all the colours on your terminal.

Braiam
  • 35,991
SSD
  • 139