12 January 2016

Mac: How to Customize your Terminal Prompt

The default command line prompt in Mac OS X is something like:

ComputerName:CurrentDirectory User$

That would look something like this when the Terminal has been launched:

MacBook:~/Desktop Admin$

Not too bad, but kind of boring, and not the best, right? It’s quite easy to change though, and you can really customize your bash Terminal prompt to look like almost anything you want.

We’re going to assume you use the default bash shell, so you’ll be editing either the file .bashrc, .bash_profile, or .profile if you installed fink.

Thus, to start customizing the bash prompt, at your current Terminal prompt, type the following to load the appropriate profile into nano text editor:

nano .bashrc

Yes, you can change that to .bash_profile or .profile depending on your specifics:

nano .bash_profile

You’ll probably be presented with a plain file, so type the following onto a line in the terminal to get started:

export PS1=" "

It’s between those quotation marks that your bash prompt customization occurs.

Between the quotation marks of export PS1=” “, you can add the following lines to customize your Terminal prompt:
\d – Current date
\t – Current time
\h – Host name
\# – Command number
\u – User name
\W – Current working directory (ie: Desktop/)
\w – Current working directory with full path (ie: /Users/Admin/Desktop/)

(Note if you just want to use a custom bash prompt one-off or to test out the appearance of changes before setting them in the bash profile, you can simply use the export commands, the change will take effect immediately with the export command but will be abandoned when that Terminal session is ended.)

So, let’s take a few examples. Perhaps you want your Terminal prompt to display the User, followed by the hostname, followed by the directory, then the appropriate .bashrc entry would be:

export PS1="\u@\h\w $ "

which will look like the following when rendered at the actual bash prompt:

Admin@MacBook~Desktop/ $

Cool huh? You can also change the prompt itself to anything, it doesn’t have to be the $ sign, simply replace it with whatever else you’d like to use, : for example would be:

export PS1="\u@\h\w: "

which is the same as above, but : instead of $

Admin@MacBook~Desktop/: "

So, play around and see what you prefer. My personal favorite is the following:

export PS1="\W @ \h [\u] $ "

This renders looking the currently active directory (PWD), the hostname of the computer, and the username of the current user, looking like the following:

/System @ MacBookPro [Paul] $

No comments: