Wednesday, July 11, 2012

Colors in terminal...

I'm using terminal for so long time and I was never thinking about the number of supported colors in the terminal. Probably because I never use them. But, it turns out that the number is very low, only 8 colors. You can check it using the following command:
tput colors
Probably you'll get number 8, for 8 colors. I found out this reading feature list for Fedora 18. Even more interestingly, they mention that MacOS X Lion supports 256 colors in terminal by default.

So, naturally, I decided to try to see if I can turn on support for 256 colors, and it seems to work. In order to have so many colors first you have to define appropriate terminal. That one was easy, it turns out there is terminal definition under the name xterm-256color. So, set your terminal to that value, i.e.:
export TERM=xterm-256color
And you can use 256 colors. :) Ok, but how to check if there are really 256 available colors? One way is to use tput command again:
$ tput colors
256
But how to see what colors are there? With a bit of googling it turns out that background color can be set with the following command:
tput setaf NUM
that command will set background to NUM. So, a small for loop will print all 256 colors:
for i in {0..255}; do tput setab $i; echo -n "    "; done; tput setab 0; echo
Actually, the last tput and echo commands are so that terminal is brought back to working state. :)

No comments:

About Me

scientist, consultant, security specialist, networking guy, system administrator, philosopher ;)

Blog Archive