Modify the Linux/Centos6 terminal command prompt color

Source: Internet
Author: User

I believe many people are tired of reading the color of Linux Command Prompt which has become unchanged. Most people either use the default green or set it to green when using PUTTY, I don't know if someone thinks that the prompt can be set to another color. This article describes how to set the command prompt variable ps1.

1. Introduction to PS1 Variables

PS1 is an environment variable for Linux end users. It is used to describe the settings of command line prompts.

You can use the man bash command to view the bash manual, find the special characters supported by the variable, and the significance of these special characters:

  1. \ A # anASCIIbellcharacter (07)
  2. \ D # thedatein "WeekdayMonthDate" format (e.g., "TueMay
  3. 26 ")
  4. \ D {format}
  5. # Theformatispassedtostrftime (3) andtheresultis
  6. # Insertedintothepromptstring; anemptyformatresults
  7. # Inalocale-specifictimerepresentation.Thebracesare
  8. # Required
  9. \ E # anASCIIescapecharacter (033)
  10. \ H # thehostnameuptothefirst '.'
  11. \ H # thehostname
  12. \ J # thenumberofjobscurrentlymanagedbytheshell
  13. \ L # thebasenameoftheshell 'sterminaldevicename
  14. \ N # newline
  15. \ R # carriagereturn
  16. \ S # thenameoftheshell, thebasenameof $0 (theportion
  17. # Followingthefinalslash)
  18. \ T # thecurrenttimein24-hourHH: MM: SSformat
  19. \ T # thecurrenttimein12-hourHH: MM: SSformat
  20. \ @ # Thecurrenttimein12-houram/pmformat
  21. \ A # thecurrenttimein24-hourHH: MMformat
  22. \ U # theusernameofthecurrentuser
  23. \ V # theversionofbash (e.g., 2.00)
  24. \ V # thereleaseofbash, version + patchlevel (e.g., 2.00.0)
  25. \ W # thecurrentworkingdirectory, with $ HOMEabbreviated
  26. # Withatilde (usesthevalueofthePROMPT_DIRTRIMvari-
  27. # Able)
  28. \ W # thebasenameofthecurrentworkingdirectory, with $ HOME
  29. # Abbreviatedwithatilde
  30. \! # Thehistorynumberofthiscommand
  31. ### Thecommandnumberofthiscommand
  32. \ $ # IftheeffectiveUIDis0, a #, otherwisea $
  33. \ Nnn # thecharactercorrespondingtotheoctalnumbernnn
  34. \\# Abackslash
  35. \ [# Beginasequenceofnon-printingcharacters, whichcocould
  36. # Beusedtoembedaterminalcontrolsequencebeyond
  37. # Prompt
  38. \] # Endasequenceofnon-printingcharacters

Below I will explain the common special characters in Chinese:

  1. \ D: # indicates the date, in the format of weekdaymonthdate, for example, "MonAug1"
  2. \ H: # complete host name.
  3. \ H: # Only the first name of the host is used. In the preceding example, the name is fc4, and the name of. linux is omitted.
  4. \ T: # The display time is in the 24-hour format, for example, HH: MM: SS.
  5. \ T: # The display time is in the 12-hour format.
  6. \ A: # The display time is in the 24-hour format: HH: MM.
  7. \ U: # account name of the current user
  8. \ V: # BASH version information
  9. \ W: # complete working directory name. In the home directory ~ Replace
  10. \ W: # Use basename to get the working directory name. Therefore, only the last directory is listed.
  11. ##: # Commands
  12. \ $: # Prompt character. If it is root, the prompt is: #, and the normal user is: $
2. color settings

In PS1, set the Character Sequence color format to \ [\ e [F; Bm \]

"F" indicates the font color, ranging from 30 ~ 37; "B" indicates the background color, numbered 40 ~ 47.

Next, let's look at the color table:

  1. Foreground background color
  2. ------------------------
  3. Black 3040
  4. Red 3141
  5. Green 3242
  6. Yellow 3343
  7. Blue 3444
  8. 3545 purple red
  9. 3646 blue
  10. White 3747

Result control code:

  1. Code meaning
  2. -------------------------
  3. 0OFF
  4. 1 highlighted
  5. 4 underline
  6. 5 flashes
  7. 7. Reverse display
  8. 8 invisible
3. Example

Let's use the following example to parse the PS1 variable to set a command prompt with a color:

PS1 = "\ [\ e [32m \] ###\ [\ e [1; 31m \] \ u @ \ [\ e [36m \] \ h \ w] \ $ \ [\ e [m \"

Note:

'\ [\ E [32m \]' is used to set the color of '#\##' to green, #\# # indicates the number of commands currently running.

'[\ E [31m \]' sets '\ u @' to red and highlighted. Multiple numbers must be separated by semicolons. \ U @ is the username of the current logon followed by a '@' symbol.

'\ [\ E [36m \]' sets '\ h \ W' to blue, and \ h indicates the first host name. If the host name is centos6.lampbo.org, centos6 is displayed; \ w displays the complete absolute path.

'\ $' Indicates the character. If it is root, the prompt is: #, and the normal user is: $.

'\ [\ E [m \]' to disable the color settings. If you do not have this, your command prompt, including the output from the command prompt, is the same as the last color setting.

To maintain the variables you just set during startup and logon, you need to add the PS1 settings to The. bashrc file in the user's home directory.

Additional example:

(1): PS1 = "\ e [1; 32m \ u \ e [m \ e [1; 33m @ \ e [m \ e [1; 35m \ h \ e [m: \ w \ $"

(2) EDIT. bashrc and add the following content:

  1. C_1 = "\ [\ e [0 m % \]"
  2. C0 = "\ [\ e [30 m % \]"
  3. C1 = "\ [\ e [31 m % \]"
  4. C2 = "\ [\ e [32 m % \]"
  5. C3 = "\ [\ e [33 m % \]"
  6. C4 = "\ [\ e [34 m % \]"
  7. C5 = "\ [\ e [35 m % \]"
  8. C6 = "\ [\ e [36 m % \]"
  9. C7 = "\ [\ e [37 m % \]"
  10. PS1 = "$ c0 ****** $ c1 \ w $ c2 ***** $ c3 <\ u @ \ h> $ c4 ****** $ c5 \! $ C6 ***** $ c7 \ t $ c1 ***** \ n $ c2 \ $ c_1 "; exportPS1

I'm not very excited after reading this article. Please change your command prompt.

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.