|
|
发表于 2005-2-20 23:04:36
|
显示全部楼层
这个可能是你需要的
Prompting variables
If you have seen enough experienced UNIX users at work, you may already have realized that the shell's prompt is not engraved in stone. Many of these users have all kinds of things encoded in their prompts. It is possible to put useful information into the prompt, including the date and the current directory. We'll give you some of the information you need to modify your own here; the rest will come in the next chapter.
Actually, bash uses four prompt strings. They are stored in the variables PS1, PS2, PS3, and PS4. [11]The first of these is called the primary prompt string; it is your usual shell prompt, and its default value is "\s-\v\$ ". [12]Many people like to set their primary prompt string to something containing their login name. Here is one way to do this:
[11] PS3 was not defined in bash versions prior to 1.14.
[12] In versions of bash prior to 2.0, the default was "bash\$ ".
PS1="\u—> "
The \u tells bash to insert the name of the current user into the prompt string. If your user name is alice, your prompt string will be "alice-->". If you are a C shell user and, like many such people, are used to having a history number in your prompt string, bash can do this similarly to the C shell: if the sequence \! is used in the prompt string, it will substitute the history number. Thus, if you define your prompt string to be:
PS1="\u \!—> "
then your prompts will be like alice 1-->, alice 2-->, and so on.
But perhaps the most useful way to set up your prompt string is so that it always contains your current directory. This way, you needn't type pwd to remember where you are. Here's how:
PS1="\w—> "
Table 3.6 lists the prompt customizations that are available. [13]
[13] \[ and \] are not available in bash versions prior to 1.14. \a, \e, \H, \T, \@, \v, and \V are not available in versions prior to 2.0.
Table 3.6. Prompt String Customizations
Command
Meaning
\a
The ASCII bell character (007)
\d
The date in "Weekday Month Day" format
\e
The ASCII escape character (033)
\H
The hostname
\h
The hostname up to the first "."
\n
A carriage return and line feed
\s
The name of the shell
\T
The current time in 12-hour HH:MM:SS format
\t
The current time in HH:MM:SS format
\@
The current time in 12-hour am/pm format
\u
The username of the current user
\v
The version of bash (e.g., 2.00)
\V
The release of bash; the version and patchlevel (e.g., 2.00.0)
\w
The current working directory
\W
The basename of the current working directory
\#
The command number of the current command
\!
The history number of the current command
\$
If the effective UID is 0 print a #, otherwise print a $
\nnn
Character code in octal
\\
Print a backslash
\[
Begin a sequence of non-printing characters, such as terminal control sequences
\]
End a sequence of non-printing characters
PS2 is called the secondary prompt string; its default value is >. It is used when you type an incomplete line and hit RETURN, as an indication that you must finish your command. For example, assume that you start a quoted string but don't close the quote. Then if you hit RETURN, the shell will print > and wait for you to finish the string:
$ echo "This is a long line, # PS1 for the command
> which is terminated down here" # PS2 for the continuation
$ # PS1 for the next command
PS3 and PS4 relate to shell programming and debugging. They will be explained in Chapter 5, and Chapter 9. |
|