Customizing Your Bash Prompt

As an aspiring full stack developer, I spend a lot of time in terminal (iTerm2 specifically). In fact, I actually prefer to work in terminal rather that with the OS X gui. With that said, I want to share some of the things I've learned in regards to customizing your bash prompt.

First up try this;

$ env

The env command is going to dump your current bash session's environment variables. Right now, since the focus is on customizing our prompt, we're focused on PS1 which stands for Prompt String One. Likely, PS1 will be set to a default resembling \h:\W \u$. Here, \h returns your computer's name. \W, returns your home directory. And \u returns the current user. Since I'm the only one on my machine, my machine's name, and my username are pretty useless. In fact, the actually clutter up prime real estate. So, for my purposes, I've chosen to only retain my working directory. For fun, I did explicitly add a name as well. (I'll post the code in a moment since I'm not done just yet.)

So, just like any variable, you can reset PS1 like this:

$ PS1="what ever you want here"

However, if you want the changes to take affect each time you start a new session you're going to need to edit your bash profile. I have sublime set up to open files from iTerm (see instructions here) so I can pull up mine for editing like so:

$ sublime .bash_profile

From there,  I simply append the following:

export PS1="[Red Pill@ \W]$"

I happen to like "The Matrix", hence the 'red pill' reference. Again, \W will add my current directory, and I've also decided to keep the default $ at the end.

I fibbed though, I actually added more than that. Since the bash I/O stream is white by default, I wanted to add some color to my prompt to help visually differentiate the prompt from the I/O stream. my PS1 variable is actually set like so:

export PS1="\[\033[0;95m\][Red Pill@ \W]$ \[\033[0m\]"

The only difference is the color encoding which wraps my prompt text, effectively setting the color, then turning it off. For a list of colors, you can refer here.

Hope that helps, and feel free to contact me through my form if you have questions!

Written on December 31, 2013