Changing Your Shell Prompt

The first thing I wanted to do with my new MAC was change my shell prompt. Your shell prompt setting is stored in the PS1 variable.

How do I view my PS1 variable?

$ echo $PS1

Example
By default, on my MAC, the command prompt was set to:

$ echo $PS1
\h:\W \u\$

Result

Server:~ Tommy$

What do all these letters mean?

\h:\W \u\$
  • \h – Hostname
  • \W – Current working directory
  • \u – Current username

Is there a full list of attributes for the PS1 variable?
Here are the ones I was able to find.

  • \t – time
  • \d – date
  • \n – newline
  • \s – Shell name
  • \W – Current working directory
  • \w – The full path of the current working directory.
  • \u – Current username
  • \H – Display FQDN hostname
  • \h – Hostname
  • \# – The command number of this command.
  • \! – The history number of the current command

Example using all attributes

Server:~ Tommy$ export PS1='\t:\d:\n:\s:\W:\w:\u:\H:\h:\#:\!\$'
00:24:58:Thu Feb 10:
:-bash:~:~:Tommy:Server.local:Server:4:503$

To add colors to the shell prompt, use the following export command syntax:

$ export PS1='\e[x;ym $PS1 \e[m'
  • \e[ Start color scheme
  • x;y Color pair to use (x;y)
  • $PS1 is your shell prompt
  • \e[m Stop color scheme

List of Color codes

0;30 Black
0;34 Blue
0;32 Green
0;36 Cyan
0;31 Red
0;35 Purple
0;33 Brown
0;34 Blue
0;32 Green
0;36 Cyan
0;31 Red
0;35 Purple
0;33 Brown

Example

Red

$ export PS1='\e[0;31m[\h:\W \u]\$\e[m '
Server:~ Tommy$

Blue

$ export PS1='\e[0;34m[\h:\W \u]\$\e[m '
Server:~ Tommy$

Let’s say you wanted to get fancy with your colors. I’ll make my username blue, my hostname green, my current directory cyan and the time red.

$ export PS1='[\e[0;34m\]\u\[\e[m\]@\[\e[0;32m\]\h\[\e[m\] \[\e[0;36m\]\W\[\e[m\] \[\e[0;31m\]\t\[\e[m\]]$ '

Result

[Tommy@Server ~ 17:34:51]$

Now to create my final shell prompt.
<username>@<hostname> <current directory> <current time> and be the color red.

$ export PS1='\e[0;31m[\u@\h \W \t]\$ \e[m'

Result

[Tommy@Server ~ 15:42:42]$

Now I need to make the changes permanent.

When Bash is invoked as an interactive login shell, it first reads and executes commands from the file /etc/profile (global) and then /etc/bashrc (global), if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login,  ~/.profile and ~/.bashrc (in that order) within your personal account, and reads and executes commands from the first one that exists and is readable.

Global settings
/etc/profile
/etc/bashrc

Personal settings
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc

Now to make the new command prompt permanent
In my case, the default PS1 variable was located within /etc/bashrc, which is a global setting. To change this setting, I am going to append export PS1=”\e[0;31m[\u@\h \W \t]\$ \e[m “ to .bash_profile, which is a personal setting that will overwrite the global setting at login. If you already have any of the above personal setting bash files, go ahead and append to it, or create .bash_profile like I did.

[Tommy@Server ~ 15:42:42]$ vi ~/.bash_profile
export PS1="\e[0;31m[\u@\h \W \t]\$ \e[m "

To apply the new settings, you can either log out and log back in, or source ~/.bash_profile.

[Tommy@Server ~ 15:42:42]$ source ~/.bash_profile