Custom Bash Prompt on Ubuntu 14.04

I am not a fan of the default bash prompt in Ubuntu. I find that having the full path wastes space. Just the current directory is enough for me. If I need more pwd is fine.

For my prompt I knew wanted to have the following:

  • username
  • host name
  • current directory
  • color scheme

In addition, I thought the time could be useful when I am switching between tabs and want to figure where I was working at a glance or I want to see how long a task took.

Here is the default bash prompt on Ubuntu:

Default Bash Prompt on Ubuntu

And here is my custom prompt:

My Custom Bash Prompt

How I Customized My Bash Prompt

I picked up some ideas on how to handle the colors from this guide. That guide also has some interesting bash prompt ideas for git projects if you are interested.

I updated my .bashrc file in my home directory and made a copy in case there were any problems.

cd ~  
cp .bashrc .bashrc-backup  

Then I edited my existing .bashrc file.

gedit .bashrc  

First, I commented out places where the prompt was already defined. That would be lines starting with PS1.

#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)}\u@\h:\w\$ '

Note: I commented out the if/else statement completely since the only thing it was doing was defining the prompt. It caused issues for me if I only commented out the lines starting with PS1 there.

#PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

With that taken care of, I defined my own version of the prompt at the bottom of the document.


export PS1="$Grey[$Color_Off$Dark_Blue\T$Color_Off$Grey]$Color_Off $Light_Blue\u$Color_Off$Grey@$Color_Off$Light_Blue\h$Color_Off $Grey[$Color_Off$Purple\W$Color_Off$Grey]$Color_Off\\$ "  

I defined variables for each color I used and a variable for escaping any color. when defining the prompt I needed to include $ before each variable name and had to make sure to escape each color when I was finished with it.

I used several escape sequences that allowed Bash to fill in the appropriate information in the prompt:

'\T' is the time.
'\u' is the username.
'\h' is the hostname.
'W' is the path.

There are a lot more escape sequences but those are the ones I used in mine.

That should give you an idea of what I am doing. There is a lot more you can do and there are good resources out there. Here are a few to get you started:

Tyler Walters

Written by Tyler Walters

Tyler Walters is a software engineer in Phoenix, Arizona. He is married to a beautiful woman and has three incredible daughters. He is interested in game development, writing, beer, and hot wings.