Colors in the command prompt
by Guido Socher
![article image](http://www.linuxfocus.org/common/sart/2000-02-14:5.gif)
All terminal windows for Linux understand Ansi color codes
and with these codes it is possible to have colors in the shell prompt.
The color codes are:
- Foreground colors: 30=black, 31=red, 32=green, 33=yellow, 34=blue
35=purple, 36=turquoise, 37=white
- Background colors: 0=transparent, 40=back, 41=red, 42=green, 43=yellow, 44=blue
45=purple, 46=turquoise, 47=white
The syntax to print the codes is "Esc[background;foreground;1m" for
bold print and "Esc[background;foreground m" for normal print.
The Esc is a literal Esc character (octal 33). The coloring is switched off with
"Esc[m"
This command prints e.g Linux in yellow on a red background:
/bin/echo '\033[41;33;1m Linux \033[m'
Just try it out copying the command into the next shell.
To insert special characters such as (a literal Esc) into the shell prompt
you must include them in %{ %} for tcsh and \[ \] for bash
This gives e.g the tcsh prompt that you can see in the picture above:
set prompt='%{^[[44;33;1m%}%!\-%n@%m%{^[[m%} \n%{^[[44;37;1m%}(%~)%#%{^[[m%} '
A yellow prompt for bash is e.g:
PS1='\[^[[40;33;1m\]\u@\h:\w\$\[^[[m\] '
In both cases the literal Esc character is shown as ^[. Note: You can not
copy and paste these lines from the web browser to the shell. To get a literal
Esc in Vi you type crtl-v Esc and in Emacs this is crtl-q Esc.
Happy color command prompts!
|