Change the color of the PS1 variable.

Source: Internet
Author: User
Tags printable characters

Change the color of the PS1 variable.

2016.1.11 today I learned how to change the color of PS1. How to add the PS1 variable to find the file (. bash_profile) or bashrc
Export PS1 = "\ [\ e [32; 1m \] Test $ PWD> \ [\ e [0m \]"
# Display green, where Test adds additional content to be displayed
Export PS1 = "\ [\ e [35; 1 m \] Test $ PWD> \ [\ e [0 m \]


# Show red with reference blog http://blog.chinaunix.net/uid-13954789-id-3137184.html

We can make the prompt color by configuring the PS1 variable. In PS1, configure the Character Sequence color format as follows:

\ [\ E [F; Bm \]

Basically, it is a numerical value between "\ e [" (escape square brackets) and "m. If more than one numeric code is specified, separate them with semicolons.

Where F is the font color, numbered 30 ~ 37; B is the background color, numbered 40 ~ 47.

You can use \ e [0 m to disable color output. In particular, when B is set to 1, the highlighted and bold text is displayed. See the following color table and code table.

Color Table

Foreground background color

---------------------------------------

30 40 black

31 41 red

32 42 green

33 43 yellow

34 44 blue

35 45 purple

36 46 cyan

37 47 white

Code meaning

-------------------------

0 OFF

1 highlighted

4 underline

5 flashes

7. Reverse display

8 invisible

If you want to configure the terminal prompt style, set $ PS1 in ~ /. Specify bahrc.

"\ E [0 m"

If the numeric code is set to zero, it notifies the terminal to reset the foreground, background, and bold configurations 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:

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, if you like the black-green text, you can set the numbers to 32 and 40 respectively. 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; 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 reconfigure 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 the background color "40", 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 pretty prompt line, if the command you typed just arrived at the rightmost end of the terminal, it 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 \]"

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.