Home / Article / Changing the color of the BASH shell prompt

Changing the color of the BASH shell prompt

It’s possible to change the contents and color of the BASH shell prompt. My SSH sessions use a white background and I find the default [chris@localhost ~] style a little boring. I also find it useful to have the prompt green for ordinary user sessions, and red for root sessions so it’s easy to see how much damage you can do by colour alone.

Changing your own bash shell prompt

To change the colour of the BASH shell prompt for yourself only, you can edit your ~/.bashrc file by adding the following to the end:

if [ $TERM = 'xterm' ]
then
        PS1="\[\e[32;1m\]\u@\h:\[\e[34;1m\]\w$ \[\e[0m\]"
else
        PS1='\u@\h \w \$ '
fi

Then run:

. ~/.bashrc

to load the settings. Now your shell prompt will be green.

It’s possible to change it to other colours and add additional information to the shell prompt. Run man bash and locate the section titled “Prompting” for more information.

Changing the root bash shell prompt

This is the same principle as the above, but log in as root first and add the following to the end of root’s ~/.bashrc:

if [ $TERM = 'xterm' ]
then
    PS1="\[\e[31;1m\]\u@\h:\[\e[34;1m\]\w$ \[\e[0m\]"
else
    PS1='\u@\h \w \$ '
fi

Now do . ~/.bashrc and the prompt will be red.

Changing the default setting for new users

You can change the bash shell prompt for new users that are created by adding the green prompt (or whatever you choose to do with it) to /etc/skel/.bashrc. When you create a new user with the command useradd -m [username] then the files in /etc/skel will be copied to the newly created user home directory.

Making the change global

If you want to make this change apply to all existing users (unless otherwise overridden by their local ~/.bashrc file) you need to edit the /etc/bashrc file.