\[
and \]
sequences are used to encapsulate non-printing characters. An ANSI escape code begins with an octal 033
(the code generated by the esc key), followed by an optional character attribute, followed by an instruction. For example, the code to set the text color to normal (attribute = 0) black text is \033[0;30m
. lists available text colors. Notice that the colors are divided into two groups, differentiated by the application of the bold character attribute (1), which creates the appearance of “light” colors.Table 13-5. Breakdown of Complex Prompt String
Sequence | Action |
---|---|
| Begins a non-printing character sequence. The real purpose of this is to allow |
| Store the cursor position. This is needed to return to the prompt location after the bar and clock have been drawn at the top of the screen. Be aware that some terminal emulators do not honor this code. |
| Move the cursor to the upper-left corner, which is line 0, column 0. |
| Set the background color to red. |
| Clear from the current cursor location (the top-left corner) to the end of the line. Since the background color is now red, the line is cleared to that color, creating our bar. Note that clearing to the end of the line does not change the cursor position, which remains at the upper-left corner. |
| Set the text color to yellow. |
| Display the current time. While this is a “printing” element, we still include it in the non-printing portion of the prompt, because we don’t want |
| Turn off color. This affects both the text and the background. |
| Restore the cursor position saved earlier. |
| End the non-printing characters sequence. |
| Prompt string. |
Obviously, we don’t want to be typing that monster all the time, so we’ll want to store our prompt someplace. We can make the prompt permanent by adding it to our .bashrc file. To do so, add these two lines to the file:
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "
export PS1
Believe it or not, much more can be done with prompts involving shell functions and scripts that we haven’t covered here, but this is a good start. Not everyone will care enough to change the prompt, since the default prompt is usually satisfactory. But for those of us who like to tinker, the shell provides an opportunity for many hours of trivial fun.