Change the color of the PS1 variable

Source: Internet
Author: User
Tags word wrap

2016.1.11 today learned to change the color of PS1, how to add PS1 variable to find a file (. bash_profile), or BASHRC
Export ps1= "\[\e[32;1m\]test $PWD > \[\e[0m\]"
#显示绿色, where test is an additional addition to the content to be displayed
Export ps1= "\[\e[35;1m\]test $PWD > \[\e[0m\]

#显示红色 have a reference blog

We can make a prompt color by configuring the PS1 variable. The format for configuring character sequence colors in PS1 is:

\[\e[f; Bm\]

This is basically a numeric value sandwiched between "\e[" (escaping the opening brackets) and "M". If you specify more than one numeric code, separate them with semicolons.

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

The color output can be turned off by \e[0m, especially when B is 1 o'clock and the highlighted text is displayed, see the Color table and the code table below.

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



1 highlighting

4 Underline

5 Flashing

7 Anti-white display

8 Not visible

If you want to configure the terminal prompt style, just specify the $PS1 in ~/.BAHRC


If a numeric code is specified as zero, he notifies the terminal to reset the foreground, background, and bold configuration to their default values. You may use this code at the end of the cue line to make the text you type non-colored. Now, let's take a look at these color codes. Please note the following screenshot results:

To use this table, first find the color you want to use, and then look for the corresponding foreground number (30-37) and background number (40-47). For example, if you prefer a black-green word, you can set the numbering to 32 and 40, respectively. Then open your cue line definition and add the appropriate color code to it. The following definitions:

Export ps1= "\w>"


Export ps1= "\e[32;40m\w>"

So far, the cue line has been pretty good, but it's still not perfect. After bash shows the working directory, we need to reconfigure the colors to normal values using the "\e[0m" sequence.

Export ps1= "\e[32;40m\w> \e[0m"

This definition will show a nice green cue line, but we still need to do some cleanup work. We don't need to include a background color configuration of "40" because he configures the background as black, and black is the default color. In addition, Green is still very dark; we fix 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 nonprinting characters in the dedicated bash escape sequence "\[" and "\". These two sequences inform bash that the enclosed characters do not occupy any space on the line, so that the word wrap continues to function properly. There are no two escape sequences, although you have a nice hint line, but if you type a command that happens to the right end of the terminal, it will cause confusion. Here's our final cue line:

Export ps1= "\[\e[32;1m\]\w> \[\e[0m\]"

Don't worry. Use several colors in the same cue line, like this:

Export ps1= "\[\e[36;1m\]\[email protected]\[\e[32;1m\]\h> \[\e[0m\]"

Change the color of the PS1 variable

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: 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.