Customize the Prompt

Change the Prompt

Many shells allow you to change the terminal prompt, making it more useful for you or just making it less of a distraction. The prompt I have been using for this entire tutorial has been a very bland "%" symbol. Your prompt is probably very different and you can customize it. I will show you how to customize a bash shell prompt with one simple command. However to get the special options you have to use special characters and here is the list:

\d - Current date
\t - Current time
\h - Host name
\# - Command number
\u - User name
\W - Current working directory (ie: Desktop/)
\w - Current working directory, full path
     (ie: /Users/Admin/Desktop)

Thanks to osXdaily for the list.

All you need to do is change the PS1 environment variable. I mentioned enviornment variables before but I did not show you how to edit them. Here is how it is done:
export VARIABLE_NAME="value here"

Lets try some out!

% export PS1="> "
> export PS1="%> "
%> export PS1="\t % "
14:20:39 % export PS1="% "
% export PS1="\u % "
joe % export PS1="\u $ "
joe $ export PS1="\u@[\w] $ "
joe@[~] $ cd firstDir
joe@[~/firstDir] $ export PS1="\u[\w]$ "
joe[~/firstDir]$ cd ..
joe[~]$

There you have it. Just a simple one liner and your prompt changes immediately. Notice there is always a space at the still inside the double quotes. That is my personal preference and most people would agree. Play around with them, here are a few of my favorites:

export PS1="\u[\w]$ "
export PS1="\u@\h [\w]>"
export PS1="\d \t %"

Whatever you decide on, to make sure it stays that way every time you log into your shell include it in your start up commands. For bash just include the one export command inside the .bashrc file that should be located in your home directory.

add some colors »