Introduction

This lesson will cover how to customize your shell prompt. As you can see earlier in this course, default prompts can vary from system to system. No matter what shell you are using, you can customize your prompt by setting an environment variable.

For shells like bash, ksh, and sh, the environment variable $PS1 is used to set the primary prompt stream.

For shells like csh, tcsh, and zsh, the environment variable $prompt is used to set the prompt stream.

Customizing the Prompt with PS1

The format stream in place in environment variable determines the look and feel of your prompt. Each shell uses slightly different format strings, so consult the documentation for shell that you are using.

Let's look at customizing the bash prompt since bash is the most popular default shell for user accounts on Linux systems. The following are some of the most commonly used formatting stream options for bash.

FormatMeaning
\dDate in "Weekday Month Date" format "Tue May 26"
\hHostname up to the first dot or the short host name.
\HHostname - the fully qualified domain name of the server.
\nNewline - so that you can have a multi-line prompt.
\tCurrent time in 24-hour HH:MM:SS format
\TCurrent time in 12-hour HH:MM:SS format
\@Current time in 12-hour am/pm included.
\ACurrent time in 14-hour HH:MM format
\uUsername of the current user
\wCurrent working directory instead of the full path.
\WBasename of the current working directory
\$If the effective UID is 0 then # (super user or root account) else $ (normal user ).

For a complete listing of all the formatting options, see the bash man page.

Persist PS1 Changes

To make your customized shell prompt persist between logins, add the PS1 value to your personal initialization files.

Personal initialization files are commonly referred to as dot files, because they begin with a dot or a period. You can also use Nano, Vi or Emacs to edit your .base_profile file.

The following example use the echo to accomplish the persisting of PS1 changes

$ echo 'export PS1="[\u@\h \w]\$ "' >> ~/.bash_profile

Examples

Let's look at our current prompt string

[robin@server ~]$ echo $PS1
[\u@\h \W]\$

Let's change it, set it to username and host. You can see the prompt has been customized as robin@server $

[robin@server ~]$ PS1="\u@\h \$"
robin@server $

To add a time and full name for directories to the prompt:

robin@server $ PS1="<\t \u@\h \w>\$"
<09:23:27 robin@server ~>$ cd /var/log
<09:23:27 robin@server /var/log>$ 

To display base names for directories, use captial W

<09:23:27 robin@server ~>$ PS1="\d \t \h \W>\$"
Sun Feb 26 09:23:50 server log>$

To persist changes between logins, you need to put these change in personalization file, so let's edit .bash_profile

Sun Feb 26 09:23:50 server log>$ vi .bash_profile
# .bash_profile
 
# User specific environment and startup programs
PATH= $PATH:$HOME/bin
export PATH
 
# My custom prompt
export PS1="[\u@\h \w]\$ "
~ 
~
~