Learn Linux 13: Customizing The Prompt

Published Jan 14, 2022



Like so many things in Linux, the shell prompt is highly configurable, and while we have pretty much taken it for granted, the prompt is a really useful device once we learn how to control it.

Anatomy Of A Prompt

Our default prompt looks something like this:

[user@linux ~]$

Notice that it contains our username, our hostname, and our current working directory, but how did it get that way? Very simply, it turns out. The prompt is defined by an environment variable named PS1 (short for "prompt string 1"). We can view the contents of PS1 with the echo command.

[user@linux ~]$ echo $PS1
[\u@\h \W]\$

Don’t worry if your results are not the same as this example. Every Linux distribution defines the prompt string a little differently, some quite exotically.

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.

Here is a list of escape codes used in shell prompts:

  • \a - ASCII bell. This makes the computer beep when it is encountered.
  • \d - Current date in day, month, date format. For example, "Mon May 26."
  • \h - Hostname of the local machine minus the trailing domain name.
  • \H - Full hostname.
  • \j - Number of jobs running in the current shell session.
  • \l - Name of the current terminal device.
  • \n - A newline character.
  • \r - A carriage return.
  • \s - Name of the shell program.
  • \t - Current time in 24-hour hours:minutes:seconds format.
  • \T - Current time in 12-hour format.
  • \@ - Current time in 12-hour am/pm format.
  • \A - Current time in 24-hour hours:minutes format.
  • \u - Username of the current user.
  • \v - Version number of the shell.
  • \V - Version and release numbers of the shell.
  • \w - Name of the current working directory.
  • \W - Last part of the current working directory name.
  • \! - History number of the current command.
  • \# - Number of commands entered during this shell session.
  • \$ - This displays a $ character unless we have superuser privileges. In that case, it displays a # instead.
  • \[ - Signals the start of a series of one or more non-printing characters. This is used to embed non-printing control characters that manipulate the terminal emulator in some way, such as moving the cursor or changing text colors.
  • \] - Signals the end of a non-printing character sequence.

Adding Color

Most terminal emulator programs respond to certain non-printing character sequences to control such things as character attributes (such as color, bold text, and the dreaded blinking text) and cursor position.

Character color is controlled by sending the terminal emulator an ANSI escape code embedded in the stream of characters to be displayed. The control code does not "print out" on the display; rather, it is interpreted by the terminal as an instruction. 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.

Let’s try to make a red prompt (seen here as gray). We’ll insert the escape code at the beginning.

[user@linux ~]$ PS1="\[\033[0;31m\][\u@\h \W]\$ "

That works, but notice that all the text that we type after the prompt will also display in red. To fix this, we will add another escape code to the end of the prompt that tells the terminal emulator to return to the previous color.

[user@linux ~]$ PS1="\[\033[0;31m\][\u@\h \W]\$\[\033[0m\] "

We can create a prompt with a red background by applying a simple change to the first escape code.

[user@linux ~]$ PS1="\[\033[0;41m\][\u@\h \W]\$\[\033[0m\] "

Besides the normal (0) and bold (1) character attributes, text may be given underscore (4), blinking (5), and inverse (7) attributes. In the interests of good taste, many terminal emulators refuse to honor the blinking attribute, however.

Saving The Prompt

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[0;31m\][\u@\h \W]\$\[\033[0m\] "
export PS1


Believe it or not, there is much more that 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 the opportunity for many hours of casual fun.