Configuring the Bash prompt

Primped Prompt


Color-coding your prompt may help avoid configuration errors and data loss. We'll show you how to design your own custom shell prompt with color and control sequences.

By Heike Jurzik

Alexandre, Fotolia

ANSI control sequences for color and cursor positioning give users the ability to design a customized and functional Bash prompt to suit their own needs.

Command-line fans often find it hard to see the prompt for Xterms. If the prompt on a remote machine looks different from your home prompt, it might save you from inadvertent shutdown commands, and if unprivileged users see a green prompt and root sees a red one, you might be able to avoid configuration errors or even data loss. A shell prompt tailored to match your needs can help prevent confusion.

The default Bash prompt looks pretty much the same on most Linux distributions. SUSE Linux systems set up user@host:~> for normal users, whereas Debian uses user@host:~$. The tilde represents the user's home directory. In other words, the prompt shows the current working directory on both systems, giving the user an orientation aid. If you change directory to /var/log on Debian, the prompt will look like:

user@host:/var/log$

Most Linux systems change the prompt for the root user. The username is left out and you see a pound sign (#) instead of an angle bracket or dollar sign.

The $PS1 [1] environmental variable defines the appearance of the Bash prompt, which appears after entering a command. You can modify the $PS1 temporarily in the current shell for test purposes. When the prompt has the look and feel you need, you can make the changes permanent by modifying the Bash configuration file, ~/.bashrc. Look for the default settings for $PS1, comment out the line by inserting a pound sign (#) at the start, and add your own construction.

If one of your prompt experiments in the next couple of sections goes haywire, you can close the current Bash session and pop up a new shell or call source ~/.bashrc to restore the default prompt.

A New Format

To remove the username and hostname from the prompt and use a short but sweet prompt with a dollar sign, do the following:

chicken@samesame:~$ export PS1=$
$ls
bin/   easy/   user/

This does save space, but it also looks a little cramped.

If you would prefer a space between the dollar sign and the command, you can simply put the space and dollar sign in quotes:

$export PS1='$ '
$ ls
bin/   easy/   user/

This also applies to Bash escape sequences (see Table 1). Whenever the value for $PS1 includes an escape sequence, a blank, or a non-standard character, you will need to place the expression in quotes.

Escape sequences allow users to really polish the prompt. If you like to see your current username and hostname, you can display them by entering \u and \h. If you split these names with an "at" sign (@), the results are quite neat:

$ export PS1='\u@\h$ '
chicken@samesame$

What's missing now is the current working directory.

If you change directory without the current working directory in the prompt, you have to enter pwd ("print working directory") to find out where you are in the directory tree. With the escape sequences \w and \W, this is changed easily. The former displays the full path and can be extremely long, whereas the latter just displays the directory name (see Figure 1).

Figure 1: If you use the escape sequence \W instead of \w in the prompt, you only see the current directory name.

If you want the prompt to tell you the time, you have three display options: \t gives you a 24-hour format (HH:MM:SS) and \T (HH:MM:SS) and \@ (HH:MM) give a 12-hour format, with and without seconds. Square brackets help you format the output (Listing 1).

Listing 1: Time Display Options
01 chicken@samesame ~$ export PS1='[\t] \u@\h \w$ '
02 [20:53:03] chicken@samesame ~$ export PS1='[\T] \u@\h \w$ '
03 [08:53:06] chicken@samesame ~$ export PS1='[\@] \u@\h \w$ '
04 [08:53 ] chicken@samesame ~$

Painting by Numbers

The ANSI color codes I covered in last month's article [2] can help you paint the prompt. You need to quote all of these control sequences in \[\e[ and \] (see Table 1).

To paint the prompt with the time from the last section green but display the time in blue:

Figure 2: Painting the prompt with ANSI escape sequences.

If you prefer to see a warning when you are working as root, you can use different colors for user and root prompts. To do so, add, for example, to your user account ~/.bashrc,

export PS1='\[\e[01;32m\]\u@\h\[
\e[00m\]:\w\$ '

then add the following to the /root/.bashrc file:

export PS1='\[\e[01;31m\]\h\[\e[
00m\]:\w\$ '

Your user prompt will be plain green, whereas the root prompt will be red to keep you on your toes.

Prompts for All Occasions

Besides $PS1, other environmental variables affect the appearance of the prompt:

  • $PS2, which appears if you wrap a command line by pressing Enter but without terminating the line (because closing quotes or brackets are missing, for example),
  • $PS3, which is used by Bash's select control element, and
  • $PS4, which appears at the start of every line while you are debugging a script.

I will focus on $PS1 in this article because its siblings are too uncommon in daily work with the shell.

Cursor Moves

In addition to ANSI color control sequences are cursor positioning sequences. For example, \e[<n>A moves the cursor up and \e[<n>B moves it down <n> lines; C moves the cursor right, and D moves it left (Table 2).

In combination with color sequences, you can create practical and individual looks for the prompt. If you often find yourself at the bottom of a long path of directories (with 60 to 70 characters), you can design your prompt to display a status line, with the path and time at the top edge of the terminal and a simple prompt with a dollar sign (as a user) or pound sign (as root) (Figure 3).

Figure 3: Keeping track despite long path names and with the time, too.

To do this:

The result is always an empty command line; however, you still will know what directory you are working in and what time it is.

INFO
[1] Command Line: environmental variables: http://www.linuxpromagazine.com/issue/80
[2] Command Line: Paint Your Bash: http://www.linuxpromagazine.com/issue/83
THE AUTHOR

Heike Jurzik studied German, Computer Science and English at the University of Cologne, Germany. She discovered Linux in 1996 and has been fascinated with the scope of the Linux command line ever since. In her leisure time you might find Heike hanging out at Irish folk sessions or visiting Ireland.