Customizing the Prompt in Linux

These are my notes on customizing the prompt in linux.

My book on Cloud Computing with Amazon. It is only $0.99 and buying it helps support my family and I. Thank you in advance.



Adjusting Your Prompt
The shell prompt can be greatly configured. It can be very useful to learn about
it and that will be the purpose of this section. The default prompt will contain
information such as our username, hostname, and current working directory. The
prompt is defined by an environment variable named "PS1". We can view the
contents of "PS1" with the echo command. 

echo $PS1

From the results, we can see that "PS1" contains a few of the characters we see
in our prompt such as the brackets, the at-sign, and the dollar sign, but the
rest are a mystery. 

With this list of special characters, we can change the prompt to see the
effect. We can back up the existing prompt string so we can restore it later. To
do this, we will copy the existing string into another shell variable that we
create ourselves.

ps1_old="PS1"

We create a new variable called ps1_old and assign the value of PS1 to it. We
can verify that the string has been copied by using the echo command.

echo $ps1_old

We can restore the original prompt at any time during our terminal session by
simply reversing the process. 

PS1="ps1_old"

Now that we are ready to proceed, let us see what happens if we have an empty
prompt string.

PS1=

If we assign nothing to the prompt string, we get nothing. No prompt string at
all. The prompt is still there but displays nothing, just as we asked it to do.
We can replace it with a minimal prompt.

PS1="\$ "

Notice the trailing space within the double quotes. This provides the space
between the dollar sign and the cursor when the prompt is displayed. 

PS1="\A \h \$ "

Adding time of day is useful if we need to keep up track of when we do things.
Now this prompt is similar to what we started with.

PS1="<\u!\h \W>\$ "

Colors
Most terminal emulator programs respond to certain non-printing character
sequences to control such things as character attributes and cursor position. 

Character color is controlled by sending the terminal emulator an ANSI code
embedded in the stream of characters to be displayed. The control code does not
print out on the display, it is interpreted by the terminal as an instruction.
An ANSI escape code begins with an octal 033, escape key, followed by an
optional character attribute, followed by an instruction. For example, the code
to set the text color to normal black text is:

\033[0;30m

The following table lists the available colors.

\033[0;30m    black    
\033[0;31m    red
\033[0;32m    green
\033[0;33m    brown
\033[0;34m    blue
\033[0;35m    purple
\033[0;36m    cyan
\033[0;37m    light gray
\033[1;30m    dark gray
\033[1;31m    light red
\033[1;32m    light green
\033[1;33m    yellow
\033[1;34m    light blue
\033[1;35m    light purple
\033[1;36m    light cyan
\033[1;37m    white

We can also set the background color. 

\033[0;40m    black
\033[0;41m    red
\033[0;42m    green
\033[0;43m    brown
\033[0;44m    blue
\033[0;45m    purple
\033[0;46m    cyan
\033[0;47m    light gray

We can create a prompt with a red background:

PS1="\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\] "


Besides the normal(0) and bold(1) attributes, text may be given underscore(4),
blinking(5), and inverse(7) attributes. However, some terminal emulators refuse
to honor the blinking attribute. 

Moving the Cursor
Escape codes can be used to position the cursor. This is commonly used to
provide a clock or some other kind of information at a different location on the
screen, such as in an upper corner each time the prompt is drawn.

\033[1;cH    move the cursor to line 1 at column c
\033[nA        move the cursor up n lines
\033[nB        move the cursor down n lines
\033[nC        move the cursor forward n characters
\033[nD        move the cursor backward n characters
\033[2J        clear the screen and move cursor to upper-left corner
\033[K        clear from the cursor position to end of the current line
\033[s        store the current cursor position
\033[u        recall the stored cursor position

Obviously, we do not want to be typing these control codes in all the time so we
need to store our prompt to make it easier. We can make the prompt permanent by
adding it to our .bashrc file. To do so, add your prompt that you like followed
by "export PS1" and then save the file.