Original address: http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
-----------------
BASH Shell:change The Color of my Shell Prompt Under Linux or UNIX by VIVEK Gite on October 6, 2006
How does I change the color of my shell prompt under Linux or Unix operating systems?
Can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at The command prompt. BASH Shell is the default under Linux and Apple OS x. Your current prompt setting are stored in a shell variable called. There are variables too, like PS2, PS3 and PS4.
Tutorial Details |
|
Difficulty |
Easy (RSS) |
Root Privileges |
No |
Requirements |
Bash |
Estimated completion time |
N/A |
Bash Displays the primary prompt PS1 when it's ready to read a command, and the secondary prompt-when it PS2 I Nput to complete a command. Bash allows prompt strings to is customized by inserting a number of backslash-escaped special.
Task:display current BASH prompt (PS1)
Use the echo command to display current BASH prompt:
$ echo $PS 1
Sample outputs:
[\\u@\h \\w]\\$
This is another output from my Debian based system:
$ echo $PS 1
Sample outputs:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+ ($debian _chroot)}\u@\h:\w\$
By default the command prompt be set to [\u@\h \w]\$. The backslash-escaped special characters are decoded as follows: \u:display the current username. \h:display the hostname \w:print the base of current working directory. \$: Display # (indicates root user) if the effective UID is 0, otherwise display a $. task:modify current BASH prompt
Use the Export command to setup a new shell prompt:
$ Export ps1= "[\\u@\\h \\w \\@]\\$"
Sample outputs:
Fig.01:new Prompt in action
Where,
\h:display FQDN hostname. \@: Display the 12-hour am/pm format task:add colors to the prompt
To add colors to the shell prompt use the following export command syntax:
' \e[x;ym $PS 1 \e[m '
Where, \e[: Start color scheme. X;y:color pair to use (x;y) $PS 1:your shell prompt variable. \e[m:stop color scheme .
To set a red color prompt, type the following command:
$ export ps1= "\e[0;31m[\u@\h \w]\$ \e[m"
Sample outputs:
Fig.02:adding the colors to the prompt
A List of color codes
Color |
Code |
Black |
0;30 |
Blue |
0;34 |
Green |
0;32 |
Cyan |
0;36 |
Red |
0;31 |
Purple |
0;35 |
Brown |
0;33 |
Blue |
0;34 |
Green |
0;32 |
Cyan |
0;36 |
Red |
0;31 |
Purple |
0;35 |
Brown |
0;33 |
Note:you need to replace digit 0 and 1 to get light color version. task:how do I make the prompt setting permanent?
The
Your new shell prompt setting set by $PS 1 are temporary i.e. when you logout setting'll be lost. To have it set every time login to your workstation add above Export command to Your$home/.bash_profile file OR&N A BSP; a $HOME/.bashrc file.
$ cd
$ VI bash_profile
OR
$ vi $HOME/.BASHRC
Append The following line:
Export ps1= \e[0;31m[\u @\h \w]\$ \e[m "
Save and close the file. Say Hello to tput command
You can also use Tput command to set terminal and modify the prompt settings. For example, to display RED color prompt using a tput:
Export ps1= \[$ (tput setaf 1) \]\u@\h:\w $ \[$ (tput sgr0) \] " a List of handy tput command line options tput bold -Bold effect tput rev -Display Inverse colors Tput sgr0 -Reset Everything tput setaf {code}-Set foreground color, check color {CODE} table below for more Informat Ion. Tput Setab {code}-Set background color, check color {CODE} table below for more information. various color codes for the Tput command
Color {Code} |
Color |
0 |
Black |
1 |
Red |
2 |
Green |
3 |
Yellow |
4 |
Blue |
5 |
Magenta |
6 |
Cyan |
7 |
White |
recommend readingsHowto:change or setup your bash custom prompt (PS1)-This is tutorial explains how-to customizing a bash shell to get a goo D looking prompt, configure the appearance of the terminal and apply themes using Bashish package. Man Page-bash Tweet It Facebook it Google + it PDF it Found a error/typo on this page?