Pages

Sunday, September 18, 2011

Custom BASH Prompt in Ubuntu 11.04


Here's a quick tip for anyone who would like to customize their BASH prompt in Terminal. First, open a Terminal session, and list the contents of your home directory, making sure to include the
'-a' option to see hidden files:

    user@hostname:~$ ls -alh


You should see a file named .bashrc. This is your BASH configuration file. Open the file using gedit:

    user@hostname:~$ gedit .bashrc


Note: If, after running 'ls -alh', you don't see a .bashrc file in the results, don't worry! You can still type 'gedit .bashrc' (without the quotes) and it will create a blank .bashrc file in which you can paste the code seen below.

Once open in gedit, scroll down to around line 39, where you should see '#force_color_prompt=yes' without the quotes. Delete the '#', thus uncommenting the line and activating it.

Next, scroll down to around line 50, where you should see a section that looks similar to this:


if [ "$color_prompt" = yes ]; then  
     # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '  
     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\t\[\033[01;31m\] \d\n\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else    
     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt 
     **The default bash prompt is commented out at the beginning**

As I have noted in italics above, the default BASH prompt is commented out at the beginning, just under the 'if' statement. My own custom BASH prompt is the line below that begins with 'PS1='. You can ignore the second 'PS1=', as long as you uncommented the line I mentioned above. Feel free to copy my BASH prompt and use it, if you wish. If you'd like to construct your own, I recommend taking a look at this article from IBM for reference on the various BASH prompt variables available: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/

That's it! Happy customizing and please feel free to share a screenshot of your custom prompt in the comments below.