Useful coloured command prompt for Linux

I find it useful to have a visual prompt when I am logged into a Linux server as root (something you should rarely do).  One way of doing this is by adding colour to your command line prompt.  I decided to colour the username green if logged in as a regular user, and red if logged in as root.

It looks like this:

[username@hostname ~]$ 

and when logged in as root:

[root@hostname ~]# 

Here’s how to do it.  While logged in as yourself, edit your .bashrc file and change the export PS1 line so it reads like this:

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

Now su to root, and edit it’s .bashrc file changing export PS1 to this:

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

That easy.  You can do a lot more things with your command prompt, but this is enough for me.  Be careful when using non-printing characters (such as the colour commands above).  These should be surrounded by escape block ([ and ]).

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s