Article title: prompt line Magic-enhance system prompt line. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Daniel Robbins
Gentoo Technologies head office and CEO
Author profile
If you can easily make shell prompt lines colorful and contain more information, why do you need to stick to the monotonous
Standard shell prompt line? In this article, Daniel Robbins explains how to obtain
The shell prompts and describes how to dynamically update the title bar of the X terminal.
As a Linux/UNIX person, we have been working in shell for a long time, and in many cases, the following
This line is the one that keeps staring at us:
Bash-2.04 $
If you happen to be a super user (root), you have the right to use the following prompt line version that marks "identity:
Bash-2.04 #
These prompt lines are not very beautiful. This is no wonder that the default prompt line has been upgraded for several Linux versions.
The color and more information are added. However, even if you happen to have a new version with a good color prompt line
It cannot be perfect either. You may want to add or change several colors in the prompt line, or add or delete
Except) some information. It is not difficult to design your own colorful and decorated prompt lines from the beginning.
BASIC prompt line
In bash, you can change the value of the PS1 environment variable to set the prompt line, as shown below:
$ Export PS1 = ">"
>
The change will take effect immediately. by placing the "export" definition in your ~ The/. bashrc file can fix this change
. As long as you want, PS1 can contain any number of plain text:
$ Export PS1 = "This is my super prompt>"
This is my super prompt>
Although this is interesting, it is not particularly useful to include a large amount of static text in the prompt line. Most customized prompt line packages
Contains information such as user name, working directory, or host name. These highlights can help you compile in the shell World
Game. For example, the following prompt line shows your username and host name:
$ Export PS1 = "\ u @ \ H>"
Drobbins @ freebox>
This prompt line is especially useful for those who log on to multiple machines with multiple accounts with different names, because it can remind
You: on which machine you operate, what permissions do you have.
In the above example, we use a dedicated character sequence escaped with a backslash to notify bash
And host name insert prompt line, when these escape character sequences appear in the PS1 variable, bash will replace with a specific value
Change them. We use the sequence "\ u" (indicating the user name) and "\ H" (representing the first part of the host name ). Below is
A complete list of all the specific sequences that bash can recognize (you can find them in the "PROMPTING" section of bash man page
To this list ):
Sequence description
\ A ASCII Bell character (you can also type \ 007)
\ D "Wed Sep 06" format date
\ E ASCII escape character (you can also type \ 033)
The first part of the \ h host name (for example, "mybox ")
\ H host name (for example, "mybox.mydomain.com ")
\ J number of processes suspended by pressing ^ Z in this shell
\ L terminal device name of the shell (for example, "ttyp4 ")
\ N linefeed
\ R carriage return
\ S shell name (such as "bash ")
\ T 24-hour time (for example, "23:01:01 ")
\ T 12-hour time (for example, "11:01:01 ")
\ @ 12-hour time with am/pm
\ U User name
\ V bash version (e.g. 2.04)
\ V Bash version (including patch level )? /Td>
\ W current working directory (for example, "/home/drobbins ")
\ W "basename" of the current working directory (for example, "drobbins ")
\! Position of the current command in the history buffer zone
\ # Command number (as long as you type the content, it will accumulate at each prompt)
\ $ If you are not a super user (root), insert a "$"; if you are a super user, a "#" is displayed "#"
\ Xxx inserts an ASCII character in the format of "three-digit xxx" (replace unused digits with zero, for example, "\ 007 ").
\ Backslash
\ [This sequence should appear before the character sequence without moving the cursor (such as the color escape sequence. It enables bash to correctly calculate the line feed.
\] This sequence should appear after the non-printable character sequence.
In this way, you have learned all the special sequences escaped by backslash in bash. Please drill down these sequences a little,
Get some perceptual knowledge about them in their way of working. After some tests, add the color below.
Colorization
It is quite easy to add colors. The first step is to design a prompt line without colors. Then, all we have to do is add a terminal.
(Rather than bash) a recognizable dedicated escape sequence that enables it to display certain parts of the text in color. Standard Linux final
Terminal and X terminal allow you to set foreground (text) color and background color. if necessary, you can also enable the "bold" character.
There are eight colors available for us to choose from.
Color is selected by adding a specific sequence in PS1-basically clipped to "\ e [" (escape square brackets)
And "m. If more than one numeric code is specified, separate them with semicolons. Below is a color
Sample code:
"\ E [0 m"
If the number code is set to zero, it notifies the terminal to reset the foreground, background, and bold settings to their default values.
Value. You may use this code when the prompt line ends to make the text you typed non-color. Now, let me
Let's take a look at these color codes. Note the following screenshot results:
Color table
To use this table, first find the color you want to use, and then find the corresponding foreground number (30-37) and background
ID (40-47 ). For example, you can set the numbers to 32 and 40, respectively, if you prefer black-green text. Then open your
The prompt line defines and adds the appropriate color code to it. The following definition:
Export PS1 = "\ w>"
Changed:
Export PS1 = "\ e [32; 40m \ w>"
So far, the prompt line is not perfect even though it is already good. After bash displays the working directory,
We need to use the "\ e [0 m" sequence to reset the color to the normal value.
Export PS1 = "\ e [32; 40m \ w> \ e [0 m"
This definition will display a nice green prompt line, but we still need to do some scanning work. We do not need to include
"40" indicates the background color, because it sets the background to black, while black indicates the default color. In addition, Green is dark;
We can fix this problem by adding a "1" color code, which enables brighter bold text. In addition to this modification
In addition, we also need to include all non-printable characters in a dedicated bash escape sequence "\ [" and. The two
Serial notification bash. the enclosed characters do not occupy any space on the line, so that the automatic line feed can continue to work normally.
. Without these two escape sequences, although you have a pretty prompt line, if the command you typed happens
The rightmost end of the terminal will cause confusion. The following is our final prompt line:
Export PS1 = "\ [\ e [32; 1m \] \ w> \ [\ e [0m \]"
Don't worry about using several colors in the same prompt line, as shown below:
Export PS1 = "\ [\ e [36; 1m \] \ u @ \ [\ e [32; 1m \] \ H> \ [\ e [0m \]"
Fun in Xterm
I have explained how to add information and colors in the prompt line, but you can proceed further. You can
To dynamically update the title bar of the X terminal (such as rxvt or aterm. All you have to do is
Add the following sequence to your PS1 prompt line:
"\ E] 2; titlebar \"
You only need to replace the substring "titlebar" with the text you want to appear in the xterm title bar.
Switch ready! You do not need to use static text. you can insert the bash escape sequence into the title bar. Please refer to the following example,
It displays the username, host name, and current working directory in the title bar and defines a brief, bright green prompt line:
Export PS1 = "\ [\ e] 2; \ u @ \ H \ w \ a \ e [32; 1m \]> \ [\ e [0m \]"
This is the prompt line I used in the above screenshot results. I like this prompt line because it will show all the information
It is displayed on the title bar, rather than on the terminal. The terminal has a limit on the number of characters that a row can display. By the way,
Make sure that you use "\ [" and "\]" to enclose your title bar sequence (because this sequence is a non-print sequence for terminals ).
The problem with storing a large amount of information in the title bar is that if you use a non-graphical terminal (such as the system console), you will not be able to see these
Information. To solve this problem, you can add the following lines in your. bashrc:
If ["$ TERM" = "linux"]
Then
# We're on the system console or maybe telnetting in
Export PS1 = "\ [\ e [32; 1m \] \ u @ \ H> \ [\ e [0m \]"
Else
# We're not on the console, assume an xterm
Export PS1 = "\ [\ e] 2; \ u @ \ H \ w \ a \ e [32; 1m \]> \ [\ e [0m \]"
Fi
This bash condition statement dynamically sets the prompt line based on the current terminal settings. To achieve consistency, you must
Hope to configure your ~ /. Bash_profile, so that it can search for your ~ /. Bashrc. Make sure that your
~ The/. bash_profile file contains the following line:
Source ~ /. Bashrc
In this way, no matter whether you start a logon shell or a non-logon shell, you will receive the same prompt line.
Now, you have mastered the magic tips. Now let's have a try and make a beautiful color prompt!
Reference Resources
* Rxvt is a good and small xterm. it happens to have a large number of documents about escape sequences.
Concentrated in the "doc" directory in the tar package of the source code.
* Aterm is another terminal program based on rxvt. It supports several good visualization features, such as transparency and coloring.
* Bashish is a theme engine applicable to various terminals. Please check some good bashish items at work
Screenshot results!
Author profile
Daniel Robbins lives in Albuquerque, New Mexico. he is the president of Gentoo Technologies.
He is also Gentoo Linux (an advanced Linux running on a PC) and
Creator of the Portage system (next-generation Linux port system. He was also published by Macmillan.
Important books of Caldera OpenLinux Unleashed, SuSE Linux Unleashed, and Samba Unleashed
One of the authors. Due to the influence of Pan Man, Daniel first caught the Logo programming language when reading the second year.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service