Aug 8, 2008

Change your terminal prompt (bash)

I always get messed up with the output and the prompt. I wanted to so sth that would make the prompt distinguishable. I also hate the (increasing) length of prompt ie showing the full current path.

Ok here's what and how I did. It easy and a bit tricky.

You must have known that while starting a (bash) terminal, the terminal itself runs the script in the "~/.bashrc" file. If you didnt know, know it now.

Ok. Its straight forward, what we do now is add few lines to the end of the file. So open the file in any editer you like and add the following line at the end of the file

BLUE=$(tput setaf 4)
BOLD=$(tput bold)
RESET=$(tput sgr0)
PS1="\[$BLUE\][\u@\h]\[$RESET\] "


Here the env variable PS1 is the normal terminal prompt. FYI PS2 contains the continuation prompt in bash.
So what this says is "DISPLAY A PROMPT IN BLUE COLOR SHOWING [USERNAME@HOSTNAME] AS PROMPT".

Here is the screenshot of my (gnome-)terminal:
gnome-terminal

Zoomed
Zoomed gnome-termial

EDIT:
Here is a list of Escape Sequence that yuu can use:
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html