Thursday, June 28, 2007

How do I change the color of my shell prompt?

How do I change the color of my shell prompt?

Resolution:Since many Linux commands, programs, and utilities use a shell interface, it is some times helpful to customize the command prompt to distinguish it from the output of these commands, programs, and utilities. In Red Hat Enterprise Linux the default shell is the bash shell. The following information is for the bash shell but may work with other shells as well.

The prompt is stored in the $PS1 environmental variable. It can be displayed by typing the following:


# echo $PS1
[\u@\h \W]\$


By default the command prompt is set to: [\u@\h \W]\$ These special characters display things like hostname and current directory. For more details see the PROMPTING section in the bash man pages.


To add colors to the prompt change the prompt to '\e[x;ym $PS1 \e[m' where:
\e[ is the symbol to start and ascii code and m stops it
x;y is what colors to use, change the x and y values to use different colors
$PS1 is your prompt you can use the default shown above or your own
\e[m is another ascii break and close to tell it to stop using the new color

To set the new prompt use the command:


# export PS1='\e[0;33m[\u@\h \W]\$ \e[m '


This example sets the prompt to asci color 0;33 which is a orange color. See the below table for additional colors:

Black 0;30 White 1;37 Blue 0;34 Light Blue 1;34 Green 0;32 Light Green 1;32 Cyan 0;36 Light Cyan 1;36 Red 0;31 Light Red 1;31 Purple 0;35 Pink 1;35 Brown 0;33 Yellow 1;33 Light Gray 0;37 Dark Gray 1;30



However, this will only set it temporarily. To have it set every time you log in, add it to to the bottom of your .bachrc file in your home directory. See the below example of /home/$username/.bashrc


# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export PS1='\e[0;33m[\u@\h \W]\$ \e[m '

No comments: