Thursday, January 1, 2009

Setting Your Bash Shell Prompt

Your prompt consists of a set of characters that appear each time the shell is ready to accept a command. The PS1 environment variable sets what the prompt contains and is what you interact with most of the time. If your shell requires additional input, it uses the values of PS2, PS3, and PS4. When your Linux system is installed, often a prompt is set to contain more than just a dollar sign or pound sign. For example, in Fedora or Red Hat Enterprise Linux, your prompt is set to include the following information: your username, your hostname, and the base name of your current working directory. That information is surrounded by brackets and followed by a dollar sign (for regular users) or a pound sign (for the root user). Here is an example of that prompt:

[chris@myhost bin]$

If you change directories, the bin name would change to the name of the new directory. Likewise, if you were to log in as a different user or to a different host, that information would change.

You can use several special characters (indicated by adding a backslash to a variety of letters) to include different information in your prompt. These can include your terminal number, the date, and the time, as well as other pieces of information.

Characters to Add Information to the bash Prompt
\! Shows the current command history number. This includes all previous commands stored for your username.

\# Shows the command number of the current command. This includes only the commands for the active shell.

\$ Shows the user prompt ($) or root prompt (#), depending on which user you are.

\W Shows only the current working directory base name. For example, if the current working directory was /var/spool/mail, this value simply appears as mail.

\[ Precedes a sequence of nonprinting characters. This can be used to add a terminal control sequence into the prompt for such things as changing colors, adding blink effects, or making characters bold. (Your terminal determines the exact sequences available.)

\] Follows a sequence of nonprinting characters.

\\ Shows a backslash.

\d Displays the day name, month, and day number of the current date. For example: Sat Jan 23.

\h Shows the hostname of the computer running the shell.

\n Causes a newline to occur.

\nnn Shows the character that relates to the octal number replacing nnn.

\s Displays the current shell name. For the bash shell, the value would be bash.

\t Prints the current time in hours, minutes, and seconds (for example, 10:14:39).

\u Prints your current username.

\w Displays the full path to the current working directory.

To make a change to your prompt permanent, add the value of PS1 to your .bashrc file in your home directory (assuming that you are using the bash shell). There may already be a PS1 value in that file that you can modify. Refer to the Bash Prompt HOWTO ( for information on changing colors, commands, and other features of your bash shell prompt.

Source of Information : Linux Bible 2008 Edition

No comments: