Coloured Shell Prompts

A script to automatically generate the shell prompt colour from the username and hostname

I like to have a different coloured shell prompt for every PC, so I don't end up executing commands on the wrong machine. Previously, I have edited .bashrc manually on every new machine, but it would be nice if this was done automatically when I check out my dot files from the repository onto the machine.

If you make the following changes to your .bashrc, the colour and font-weight of the shell prompt will be chosen from a hash of your current username and the machines' hostname. My terminals have a black background. If yours have white, you might want to remove the white/yellow options and add black and brown instead, so the text shows up.

Find
if [ "$color_prompt" = yes ]; then
change the next line to
PS1='${debian_chroot:+($debian_chroot)}\[\033[`color_from_hostname`m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Then, at the end of the file add:
function color_from_hostname {
hash=`(echo $USER; hostname) | md5sum | awk '{print $1}'`
case ${hash#${hash%?}} in
"0") c="1;34" ;; # Light blue
"1") c="0;34" ;; # Dark blue
"2") c="1;32" ;; # Light green
"3") c="0;32" ;; # Dark green
"4") c="1;36" ;; # Light cyan
"5") c="0;36" ;; # Dark cyan
"6") c="1;31" ;; # Light red
"7") c="0;31" ;; # Dark red
"8") c="1;35" ;; # Light purple
"9") c="0;35" ;; # Dark purple
"a") c="1;33" ;; # Light yellow
"b") c="0;33" ;; # Dark yellow
"c") c="1;37" ;; # Light white
"d") c="0;37" ;; # Dark white
# Run out of colours. Lets have some repeats
"e") c="0;34" ;;
"f") c="1;32" ;;
esac
echo $c
}

Posted 23rd August 2009 in Computer, with 0 comments

Digg!

comments


  1. (optional)