Linux shell prompt

Source: Internet
Author: User
Tags printable characters

PS1 = '[/[/033 [01; 32 m/]/h:/[/033 [01; 34 m/]/W/[/033 [00 m/]/$'

A brief explanation:
The useful information is as follows:
PS1 = '[/h:/W]/$'
/H display the current host name
/W display the current directory
/$ Prompt
[] For beauty

The result is as follows:
[Taoyue: ~] $

Add colors.
/[/033 [01; 32 m/]: The host name is green.
/[/033 [01; 34 m/]: the current directory is blue.
/[/033 [00 m/]: set the following color to the default value.

==========================================

The following is the most comprehensive prompt description I have found:

If you can easily make shell prompt lines colorful and contain more information, why should you stick to the monotonous standard shell prompt line? In this article, Daniel Robbins explains how to obtain the shell prompt line that meets your needs and 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 line is always staring at our prompt line:

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. It is no wonder that some Linux versions have updated the default prompt line and added color and more information. However, even if you happen to have a new version with a good color line, it cannot be perfect. You may want to add or change several colors or add or delete some information in the prompt line. 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 lines contain information such as the user name, working directory, or host name. These highlights help you navigate the shell world. 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 reminds you of the permissions on which machine you are currently operating.
In the above example, we use a dedicated Character Sequence escaped with a backslash to notify Bash to insert the user name and host name into the prompt line, when these escape character sequences appear in the PS1 variable, bash replaces them with specific values. We use the sequence "/u" (indicating the user name) and "/H" (representing the first part of the host name ). The following is a complete list of all the specific sequences that Bash can recognize (you can find this list in the "prompting" section of bash man page ):
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)
/H the first part of the Host Name (such as "mybox ")
/H Host Name (for example, "mybox.mydomain.com ")
/J number of processes suspended by pressing ^ Z in this shell
/L the terminal device name of the shell (such as "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 insert 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 to gain some perceptual knowledge about them in the way they work. 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 need to do is add a dedicated escape sequence that can be recognized by the terminal (rather than bash) so that it can display some parts of the text in color. Standard Linux 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 special sequence in PS1-basically a numerical value between "/E [" (escape square brackets) and "M. If more than one numeric code is specified, separate them with semicolons. The following is a color code example:

"/E [0 m"

If the numeric code is set to zero, it notifies the terminal to reset the foreground, background, and bold settings to their default values. You may use this code when the prompt line ends to make the text you typed non-color. Now 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 number (40-47 ). For example, you can set the numbers to 32 and 40, respectively, if you prefer black-green text. Then open your prompt line definition and add the appropriate color code in it. The following definition:

Export PS1 = "/W>"

Changed:

Export PS1 = "/E [32; 40 m/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; 40 m/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 the "40" background color settings because it sets the background to black, while black is the default color. In addition, the green color is quite dark. We can fix this problem by adding a "1" color code, which enables brighter bold text. In addition to this modification, we also need to include all non-printable characters in a dedicated bash escape sequence "/[" and. The two sequences notify bash that the enclosed characters do not occupy any space on the row, so that the automatic line feed can continue to work normally. Without these two escape sequences, even though you have a very beautiful prompt line, if the command you key arrives at the rightmost end of the terminal, it will cause confusion. The following is our final prompt line:

Export PS1 = "/[/E [32; 1 m/]/W>/[/E [0 m/]"

Don't worry about using several colors in the same prompt line, as shown below:

Export PS1 = "/[/E [36; 1 m/]/u @/[/E [32; 1 m/]/h>/[/E [0 m/]"

Fun in xterm
I have explained how to add information and colors in the prompt line, but you can proceed further. You can dynamically update the title bar of the X terminal (such as rxvt or aterm) by adding special code to the prompt line. 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. Now everything is ready! You do not need to use static text. You can insert the bash escape sequence into the title bar. In the following example, the user name, host name, and current working directory are displayed in the title bar and a brief, bright green prompt line is defined:

Export PS1 = "/[/E] 2;/u @/h/W/A/E [32; 1 m/]>/[/E [0 m/]"

This is the prompt line I used in the above screenshot results. I like this prompt line because it displays all the information on the title bar, rather than on the terminal. The number of characters that the terminal can display on a line is limited. By the way, make sure that you include the sequence of your title bar with "/[" and "/]" (because this sequence is not a printed sequence for terminals ). If you are using a non-graphic terminal (such as the system console), you cannot see this 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; 1 m/]/u @/h>/[/E [0 m/]"
Else
# We're not on the console, assume an xterm
Export PS1 = "/[/E] 2;/u @/h/W/A/E [32; 1 m/]>/[/E [0 m/]"
Fi

This bash Condition Statement dynamically sets the prompt line based on the current terminal settings. To achieve consistency, you must 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 good color prompt.

Author:
Daniel Robbins
Gentoo technologies head office and CEO

==========================================
Make a description of PS1

Bash has two user prompts. The first level is that bash you often see is waiting for command input.
. The default first-level prompt is the character $ (# if it is a Super User ).
You can change your default prompt by changing the value of the bash PS1 variable, for example:
PS1 = "Please enter a command"
Specify the bash shell prompt as the specified string.

The second-level prompt is displayed when bash expects to enter more information to complete the command. The default
The second-level prompt is>. If you want to change the second-level prompt, you can set the PS2 variable
Value to achieve:
PS2 = "I need more information"
In addition, you can use special characters to define your prompt. The following list lists the most common
Special characters used.
Prompt special character code
Character meaning
/! Displays the history Number of the command.
/# Display the command number of the current command.

/$ Shows the $ operator as the prompt. If the user is root, the # is displayed.
// Display the backslash.

/D: displays the current date.

/H displays the host name.

/N print the new line.
/NNN: displays the NNN octal value.

/S displays the name of the currently running shell.
/T displays the current time.
/U displays the User Name of the current user.
/W displays the name of the current working directory.
/W displays the path of the current working directory.

These special characters can be combined into many useful prompt schemes (or can be combined into a very singular
For example, set PS1:
PS1 = "/t"
This causes the prompt to display the current time, as shown below (the prompt will not
There will be spaces ):
02:16:15

The following settings:
PS1 =/T
The prompt will look like the following:
T
This shows the importance of setting quotation marks. The following prompt string:
PS1 = "/T //"
The prompt will look like this:
02:16:30/
In this case, there is a space behind the prompt, because there is a space in the quotation marks.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.