Introduction

Your shell can be personalized quite a lot. Using settings in your .bashrc file you can change the prompt, set up aliases and much more.

Activating changes

Once you have edited your .bashrc file you will want to activate the changes without logging out and back in again. Use source ~/.bashrc to do this.

The prompt

The standard prompt is a pretty boring and basic item. You can make it more useful by configuring it to display information you need and change the colors to make it stand out.

If you do most of your work on the bash prompt as a normal user but occasionally suto root, it is nice to have a clear indication that you are root. One way to do this is to set the color of the prompt to be green for a normal user and red for root. To do this you need to edit the two .bashrc files: ~/.bashrc and /root/.bashrc
In your ~/.bashrc file add this line:

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

In your /root/.bashrc add this line

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

The first part \[\e[1;32m\] and \[\e[1;31m\] sets the color. The last part \[\e[0m\] returns the color to white. The bits in between [\u@\h \W]\$ are what give you the information username @ hostname.

In this ANSI color code, \[\e[1;32m\], the ANSI color codes are the bits inside the inner brackets: 1;32m. If there is a semicolon, ;, then the first part is the modifier and the second part is the color. In this case the 1 means bold. The colors can be any of these:

30 = Black
31 = Red
32 = Green
33 = Yellow
34 = Blue
35 = Magenta
36 = Cyan
37 = White

If you just wanted normal red you would use 31m instead of 1;31m. To reset the color back to default you use 0m.

The individual parts are as follows
\u – Username.
\h – Hostname.
\w – Current absolute path. Use \W for current relative path.
\$ – The prompt character This will be # for user root, $ for regular users).

There are lots of other escape codes you can use in your prompt. To find out more please see man bash and search for PROMPTING.

If you are new to using root

Enter this in your /root/.bashrc:

shopt -o noclobber

This noclobber prevents you from overwriting a file with a redirect by accident. If you then want to force the issue use >|filename.txt.

Put lots of useful information on the title bar

In .bashrc this gives the “username @ full hostname” and the full working directory.

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;\u@\H: \w\a\]$PS1"
    ;;
*)
    ;;
esac

Tidying up the history

You can ensure that you get no duplicates in you history file. For example, you might type ls -la a lot and don’t want your history file full of these. You can add this line to your .bashrc:

export HISTCONTROL=ignoredups

Getting IP addresses

Quite often you will need either the server’s IP or the IP you are connecting from. These functions make that information immediately available.

serverip() {
        /sbin/ifconfig eth0 | grep 'inet ' | awk {'print $2'} | sed -e s/addr://
        }

myip () {
        who | awk {'print $5'} | tr -d '(),'
        }

Now when you type either serverip or myip at the prompt you will get the relevant IP address immediately.