Tuesday, August 10, 2010

Color in bash

  • echo -e '\E[COLOR1;COLOR2mSome text goes here.'

ColorForegroundBackground
black3040
red3141
green3242
yellow3343
blue3444
magenta3545
cyan3646
white3747


Example:

  • bash$ echo -e '\E[34;47mThis prints in blue.'; tput sgr0
  • bash$ echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0
       
The tput sgr0 restores the terminal settings to normal. Omitting this lets all subsequent output from that particular terminal remain blue.

The simplest, and perhaps most useful ANSI escape sequence is bold text, \033[1m ... \033[0m. The \033 represents an escape, the "[1" turns on the bold attribute, while the "[0" switches it off. The "m" terminates each term of the escape sequence.

  • bash$ echo -e "\033[1mThis is bold text.\033[0m"
       



No comments:

Post a Comment