A More Linux-like Mac Bash Prompt

I am forever going back and forth in Terminal from my local machine (Mac Pro) to an ssh session on my Ubuntu Server. Terminal is not the easiest on the eyes to begin with but I’m particularly bothered by OS X’s default shell prompt, especially after spending time in Ubuntu’s shell.

This is what the default Mac shell prompt looks like:

Mac Prompt

And this is what my Ubuntu shell prompt looks like:

Ubuntu Prompt

A little splash of color goes a long way on tired eyes. But even the Ubuntu layout is better… on a Mac the prompt reads like this:

host:~/directory/ user$

as opposed to this on Ubuntu:

user@host:~/directory/$

At first look the difference seems negligible, but consider drilling deep into the file structure with cd… again, on a Mac:

host:~/Documents/Tutorials/server/networking/ user$

and on Ubuntu:

user@host:~/Documents/Tutorials/server/networking/$

Tell me in each example, at a glance, a) who’s session it is and b) what folder you’re currently in. On the Mac, one might be inclined to think we’re in the “user” folder and “host” is the session user. In Ubuntu there is no doubt, the prompt starts by identifying the user and the host, and ends with the current folder.

So what can we do about it?

## Adding color ##

You may or may not already have a `~/.profile` or `~/.bash_profile` on your Mac–if you do, that’s great. If not, make one (using vim, nano, TextMate or your editor of choice).

For a default Mac prompt, add this line:

PS1="[e[34m]h[e[00m]:[e[32m]w [e[34m]u[e[00m]$ "

For this outcome:

new mac bash prompt

Here is what it all means…

* `PS1` — the initial prompt string
* `[e[34m]h` — will be a blue `host`
* `[e[00m]:` — will be a white colon
* `[e[32m]w ` — will be green directories followed by a space
* `[e[34m]u` — will be the user in blue
* `[e[00m]$` — will be a white `$`

Here is a chart to help you with the colors and their corresponding numbers (courtesy of [IBM][a_110613211618]):

color chart

## Making it easier to read ##

If you’re like me and you want to be able to read the prompt logically, then you can rearrange the prompt like this:

PS1="[e[34m]u@h[e[00m]:[e[32m]w[e[00m]$ "

For this outcome:

new Ubuntu-like bash prompt

I hope that helps make your stay in Terminal a little easier.

[a_110613211618]: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/

 

Adam Merrifield

 

Leave a Reply