Change bash prompt color by setting PS1 variable under Linux

Source: Internet
Author: User
Tags word wrap

We all know that in bash, you can change the color of the prompt by PS1 variables. When the command output is long, it is often not easy to see where the output begins at the first glance, change the PS1 variable by changing it, you can change the color of the bash prompt so that you can look at the crowd and find where the output begins. Also, setting colors can make the command line more beautiful


We can change the content of Bash's prompt by setting the PS1 variable, as follows:

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2014/1007/210029_BFfh_1424781.png "style=" padding:5px;margin:10px 0px;border:1px solid RGB (221,221,221); Background:rgb (244,247,249); alt= 210029_BFfh_ 1424781.png "/>

After changing the contents of the PS1 variable, the bash prompt follows the changes. The PS1 variable can also use the backslash "\" to display the contents of a similar variable, knowing that the backslash escaped should be unfamiliar, as follows:

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2014/1007/210411_YgWm_1424781.png "style=" padding:5px;margin:10px 0px;border:1px solid RGB (221,221,221); Background:rgb (244,247,249); alt= 210411_YgWm_ 1424781.png "/>

Adding "\u" to the PS1, the user name of the current user appears in the bash prompt. There are a number of similar escapes, such as "\ T" and "\ T" for the current time (the two are slightly different in time format and "\w" represents the current directory.) The content of the specific prompt can be seen in the "prompting" section of "Man bash".


The PS1 variable can not only change the content of the prompt, but also change the color of the prompt. Just add "\e" to the PS1.

The format of the prompt color is: \e[font style; font color; background color m

The format at which the prompt color ends is: \e[0m

Font style, font color, background color Three in the middle of the use of English half-width semicolon separated, all three are digital code

?

123456789101112 字体样式代码:0        OFF1        高亮显示2        3        4        下划线5        闪烁67        反色8        不可见#有些代码没有对应的效果,就是和0一样

?

1234567891011 字体及背景颜色代码:字体    背景    颜色30      40     黑色31      41     红色32      42     绿色33      43     ***34      44     蓝色35      45     紫色36      46     青色37      47     白色


You can use the ' echo-e ' \e[x;y;zm xxx \e[0m ' command to view the display effect as follows:

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2014/1007/213433_777Z_1424781.png "style=" padding:5px;margin:10px 0px;border:1px solid RGB (221,221,221); Background:rgb (244,247,249); alt= 213433_777Z_ 1424781.png "/>

You can use ECHO-E to try it out and know what color you like.

After you have your own color and prompt, use "\e[0m" to end it so that the command does not receive the effect of the color.


But the light is still not perfect. When the command is long, more than one line, the command will not wrap, but will overwrite the current line of the prompt! As follows:

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2014/1007/214622_tIen_1424781.png "style=" padding:5px;margin:10px 0px;border:1px solid RGB (221,221,221); Background:rgb (244,247,249); alt= 214622_tIen_ 1424781.png "/>

To avoid this, surround the non-display portion of the PS1 variable with Escape "\[" and "\]", which tells Bash that the enclosed character does not occupy space on the line, so that the word wrap will work correctly. Otherwise, it will cause problems.

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2014/1007/215220_aifz_1424781.png "style=" padding:5px;margin:10px 0px;border:1px solid RGB (221,221,221); Background:rgb (244,247,249); alt= 215220_aifz_ 1424781.png "/>

This will be able to use the normal, feel that their color is very good, you should quickly write to the configuration file go to it


Below I share a ps1= "\[\e[36m\][\[\e[36m\]\u\[\e[0m\]@\h \[\e[32m\]\w\[\e[36m\]]\[\e[0m\]\\$" pasted into the/ETC/BASHRC file in the following location

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/C9/wKioL1RZzAnwHLUlAADyVJ4r9bk060.jpg "title=" 1.png " alt= "wkiol1rzzanwhlulaadyvj4r9bk060.jpg"/> Effect is as follows

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/CA/wKiom1RZy-OycSTaAACX6Q2h4Ws493.jpg "title=" 2.png " alt= "Wkiom1rzy-oycstaaacx6q2h4ws493.jpg"/>

The brackets and the username are Green and the command is too long to overwrite the previous line, without the background color defined, even if your background is white!

This article is from the "Professor" blog, please be sure to keep this source http://professor.blog.51cto.com/996189/1572141

Change bash prompt color by setting PS1 variable under Linux

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.