If you easily make the shell prompt to become colorful and bring more information, why should I stick to the monotonic standard shell prompt? In this skill, Daniel Robbins will explain how to get the shell prompt that meet your will, and explains how to dynamically update the title bar of the X terminal.
As a Linux / UNIX person, we have a long time to work in the shell, and in many cases, the following line is always staring at our tips:
BASH-2.04 $
If you happen to be a superuser (root), you have the right to use this beautiful indicator "identity" tip version:
Bash-2.04 #
These tips are not very beautiful. This is also no wonder that several Linux versions are upgraded to the default prompt line, increasing colors and more information. However, even if you just have a new version of a good color prompt, it is impossible to be perfect. You may wish to add or change several colors in the prompt line, or add (or delete) some information. It is not difficult to design your own colors from the beginning of the head.
Tipping Basics Under Bash, you can set the prompt line by changing the value of the PS1 environment variable, as shown below:
$ EXPORT PS1 = ">"
>
Changes will take effect immediately and fix this change by placing "Export" defined in your ~ / .bashrc file. As long as you wish, 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 in the prompt. Most custom prompts contain information such as user names, working directories, or hostnames. These highlight information can help you swim in the Shell world. For example, the following prompt will display your username and host name:
$ export ps1 = "u @ h>"
DROBBINS @ freebox>
This prompt is especially useful for people who log in to multiple machines with multiple different names, because it can remind you: where you are currently operating, what permissions you have.
In the example above, we used a dedicated character sequence that uses a backslash transfusion, in which the user name and hostname are inserted into the prompt line, when these escape character sequences appear in the PS1 variable, BASH is They will be replaced with specific values. We use the sequence "U" (indicating user name) and "H" (indicating the first part of the host name). Below is a complete list of all private sequences identified by Bash (you can find this list in the "Prompting" section of Bash Man Page):
Sequence Description AASCII Bell Character (You can type 07) D "WED SEP 06" format Date Eascii escape character (or type 33) h host name (eg "Mybox") h host full name (eg " MyBox.mydomain.com ") J In this shell Time (such as "23:01:01") T12 hours (such as "11:01:01") @ 12-hour system with AM / PM User name VBASH version (such as 2.04) VBash version ( Including patch levels)? / TD> W Current working directory (such as "/ home / DRobbins") "Basename" "(," "" "(such as" DRobbins ")! The current command in the historical buffer position # Command number (as long as you type content, it will be accumulated at each prompt) $ If you are not a super user (root), insert a "$"; if you are a super user, display a "#" xxx insert one Use three-digit xxx (replaced unused numbers, such as "07") ASCII characters represented by "07") [This sequence should appear before the character sequence "such as color escape sequences) does not move the cursor. It allows BASH to correctly calculate automatic wrap. This sequence should appear after a non-print character sequence. In this way, you have known all special sequences in Bash with backslash transfusion. Please exercise these sequences a little to get some sense of sensibility in their way. After you have made some tests, you start adding a color.
Color is quite easy to add color; the first step is to design a prompt without color. Then, what we have to do is added to add a terminal (not BASH) identifiable dedicated escape sequence to make it display some parts of the text in color. Standard Linux terminals and X terminals allow you to set the foreground (text) color and background color, if needed, you can also enable "bold" characters. There are eight colors for us to choose.
The color is selected by adding a dedicated sequence in PS1 - basically a digital value between "E" E ["(escape start bracket) and" M ". If you specify more than one digital code, use the semicolon to separate them. Below is an example of a color code:
"e [0m"
If you specify the numeric code to zero, it will notify the terminal to reset the foreground, background and bold settings to their default values. You may use this code at the end of the prompt line so that the text you typed is non-colored. Now let's take a look at these color code.
To use this table, first find the color you want to use, then look for the corresponding foreground number (30-37) and background number (40-47). For example, if you like a black-faced green word, you can set the number to 32 and 40, respectively. Then open your prompt definition and add the appropriate color code in it. The definition below:
Export ps1 = "w>"
Change to:
Export ps1 = "e [32; 40MW>"
So far, the prompt is already very good, but it is still not too perfect. After Bash shows the working directory, we need to use the "E [0M" sequence to reset the color to a normal value.
Export ps1 = "e [32; 40MW> E [0M"
This definition will display a beautiful green prompt, but we still need to do some sweeping work. We don't need to include "40" background color settings because it sets the background to black, and black is the default color. In addition, green is still very dark; we correct this problem by adding a "1" color code, which will enable brighter bold text. In addition to this modification, we also need to enclose all non-print characters to enclose the special BASH escape sequence "[" and "]". These two sequences notify BASH, which is not occupied by any space on the line, so that the automatic wrap can continue to work properly. Without these two escape sequences, although you have a very beautiful prompt, however, if you typing the command just arrive at the rightmost end of the terminal, it will cause chaos. Here is our final prompt:
Export ps1 = "[e [32; 1M] w> [e [0m]"
Don't worry that you use several colors in the same prompt, just like this:
Export PS1 = "[E [36; 1M] u @ [e [32; 1M] h> [e [0m]"
The fun in xterm I have explained how to add information and colors in the prompt, but you can further. You can make X-terminal (such as RXVT or ATERM)'s title bar to dynamically update by adding a dedicated code in a prompt. What you have to do is added to your PS1 prompt:
"e] 2; titlebara"
You only need to use the text "titlebar" "Titlebar" you want to appear in the xterm title bar, now everything is ready! You don't have to use static text; you can insert the BASH escape sequence into the title bar. Check out the following example, which displays the username, hostname, and the current working directory in the title bar, and define a short, bright green prompt:
Export ps1 = "[e] 2; u @ h wae [32; 1M]> [e [0m]"
This is the prompt that I used in the captain results above. I like this prompt because it displays all the information on the title bar instead of displaying how many characters can be displayed on the terminal. By the way, make sure that "[" and "]" columnize your title bar (because the terminal is in the end, this sequence is a non-printing sequence). The problem with placing a lot of information in the title bar is that if you use a non-graphics terminal (such as a system console), you can't see this. In order 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 wae [32; 1M]> [e [0m]"
Fi
This Bash condition statement will set up a dynamic settings according to the current terminal. To get consistency, you must want to configure your ~ / .bash_profile so that it is searching for your ~ / .bashrc at startup (SOURCE). Make sure you have the following rows in your ~ / .bash_profile file: Source ~ / .bashrc
In this way, whether you open a login shell or a non-login shell, you will get the same prompt.
Ok, you have mastered the tips. Now enjoy it, make a beautiful color prompt!
Reference resource
RXVT is a good and small XTERM, which has a large number of document materials on escape sequences, which are concentrated in the "doc" directory in the source TAR package. ATERM is another terminal program based on RXVT. It supports several good visual features such as transparency and coloring. Bashish is a subject engine for a variety of terminals. Please check some good feet results in the work in the work!
About the Author
Daniel Robbins live in Albuquerque in New Mexico, he is a total load and CETOKUX, and he is a Gentoo Linux (a high-end Linux that runs on a personal computer) and a portage system (next-generation port system for Linux). creator. He is also one of the important authors of Caldera OpenLinux Unleashed, SUSE Linux Unleashed and Samba Unleashed, and Samba Unleashed. Due to the influence of PAN MAN, Daniel is in the first time the second grade is in the first time, it is fascinated by the computer. This may be the reason why he later as a chief graphic artist in Sony Electronic Publishing / Psygnosis. Daniel likes his wife Mary and his newborn daughter Hadassah. You can contact Daniel via DRobbins@gentoo.org.