Colorful Bash Prompt

Custom Bash Prompt

As a sysadmin I spend a lot of time logged into other servers via SSH session. Often I have to log into systems I am not familiar with, and it is very easy to forget which one of the seven terminals that you are working in is actually logged into a remote machine.

I am certain I am not the only one that have accidentally shutdown a server while logged into it. Newby mistake, and never repeated due to this trick I learned.

Changing the Colors on Bash Prompt

NOTE: BEFORE DOING THIS MAKE A BACKUP OF YOUR .bashrc FILE. *ahem* You have been warned. Continue.

In your home directory you have a .bashrc file. On some distros (Ubuntu) your custom prompt is white, and to change that to a colored prompt you have to edit this file to enable your usual green prompt. You achieve this by opening your .bashrc file in your favorite text editor and looking for the following line:

#force_color_prompt=yes

Delete the # in that line (this is called “uncommenting” a line), and save it. If you open an new terminal you should have your username@host displayed in green.

The next step is changing your hostname to a different color.

First off choose your color, a list of the codes for different colors can be found HERE AT THE ARCH LINUX WIKI.

Once done, you edit the following line:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

All the parts in the square brackets [like these] are references to font and color for you prompt. [01;32m] is green, but since we are working with bash script, there is a backslash (like this one ==> \ ) in front of the closing square bracket. Remember that because you can break your bashrc file.

Choose your color from the archlinux wiki page, and then change the above line to look like the one below, in my case I went for purple, and I made the @ blue:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;34m\]@\[\033[1;35m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Now if the change I made above is not readily apparent to you, here it is again in non copy friendly html:

PS1=’${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;34m\]@\[\033[1;35m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘

This makes it easy for me to see when I am working on a local terminal (all the colors in my prompt!!!), and keeps me from accidentally doing something on a remote machine that I should have been doing on a local one.

Shortening the Path Display in your Bash Terminal

In Ubuntu you get to have the WHOLE directory tree displayed in your terminal. This often causes your terminal to wrap around, and can be a pain when you want to run a few piped commands.

There are a few fixes for this, but the one I am using right now I borrowed from ephemient that he posted as an answer to THIS STACKOVERFLOW QUESTION.

First off, I dumped the below code at the end of my .bashrc file:


#I ADDED THIS TO SHORTEN PWD
_PS1 ()
{
local PRE= NAME="$1" LENGTH="$2";
[[ "$NAME" != "${NAME#$HOME/}" || -z "${NAME#$HOME}" ]] &&
PRE+='~' NAME="${NAME#$HOME}" LENGTH=$[LENGTH-1];
((${#NAME}>$LENGTH)) && NAME="/...${NAME:$[${#NAME}-LENGTH+4]}";
echo "$PRE$NAME"
}

Then I put the snippet of code the calls that variable in my PS1 string like so:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;34m\]@\[\033[1;35m\]\h\[\033[00m\]:\[\033[01;34m\]$(_PS1 "$PWD" 10)\[\033[00m\]\$ '

And again to point out the change:

PS1=’${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;34m\]@\[\033[1;35m\]\h\[\033[00m\]:\[\033[01;34m\]$(_PS1 “$PWD” 10)\[\033[00m\]\$ ‘

I basically replace \w\ with $(_PS1 “$PWD” 10) to allow the shortened directory to still be nicely color prompted.

You can keep $(_PS1 “$PWD” 10) at the bottom of your .bashrc, but that will break your fancy color prompting.

You can also change the 10 to any number that you prefer. This will allow you to show 10, 20 any other chosen amount of characters in your current path.

Summary

So, this should not be too hard. If you are lazy you can replace the relevant lines in your .bashrc entirely with my lines in the copy friendly code boxes, or you can play around and build your own.

Have fun, and don’t break anything. (Especially if you have to drive out to site to fix it.)

Related posts:

  1. Connecting to a CUPS Printer in Ubuntu Lucid