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:
- \ A # anASCIIbellcharacter (07)
- \ D # thedatein "WeekdayMonthDate" format (e.g., "TueMay
- 26 ")
- \ D {format}
- # Theformatispassedtostrftime (3) andtheresultis
- # Insertedintothepromptstring; anemptyformatresults
- # Inalocale-specifictimerepresentation.Thebracesare
- # Required
- \ E # anASCIIescapecharacter (033)
- \ H # thehostnameuptothefirst '.'
- \ H # thehostname
- \ J # thenumberofjobscurrentlymanagedbytheshell
- \ L # thebasenameoftheshell 'sterminaldevicename
- \ N # newline
- \ R # carriagereturn
- \ S # thenameoftheshell, thebasenameof $0 (theportion
- # Followingthefinalslash)
- \ T # thecurrenttimein24-hourHH: MM: SSformat
- \ T # thecurrenttimein12-hourHH: MM: SSformat
- \ @ # Thecurrenttimein12-houram/pmformat
- \ A # thecurrenttimein24-hourHH: MMformat
- \ U # theusernameofthecurrentuser
- \ V # theversionofbash (e.g., 2.00)
- \ V # thereleaseofbash, version + patchlevel (e.g., 2.00.0)
- \ W # thecurrentworkingdirectory, with $ HOMEabbreviated
- # Withatilde (usesthevalueofthePROMPT_DIRTRIMvari-
- # Able)
- \ W # thebasenameofthecurrentworkingdirectory, with $ HOME
- # Abbreviatedwithatilde
- \! # Thehistorynumberofthiscommand
- ### Thecommandnumberofthiscommand
- \ $ # IftheeffectiveUIDis0, a #, otherwisea $
- \ Nnn # thecharactercorrespondingtotheoctalnumbernnn
- \\# Abackslash
- \ [# Beginasequenceofnon-printingcharacters, whichcocould
- # Beusedtoembedaterminalcontrolsequencebeyond
- # Prompt
- \] # Endasequenceofnon-printingcharacters
Below I will explain the common special characters in Chinese:
- \ D: # indicates the date, in the format of weekdaymonthdate, for example, "MonAug1"
- \ H: # complete host name.
- \ 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.
- \ T: # The display time is in the 24-hour format, for example, HH: MM: SS.
- \ T: # The display time is in the 12-hour format.
- \ A: # The display time is in the 24-hour format: HH: MM.
- \ U: # account name of the current user
- \ V: # BASH version information
- \ W: # complete working directory name. In the home directory ~ Replace
- \ W: # Use basename to get the working directory name. Therefore, only the last directory is listed.
- ##: # Commands
- \ $: # 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:
- Foreground background color
- ------------------------
- Black 3040
- Red 3141
- Green 3242
- Yellow 3343
- Blue 3444
- 3545 purple red
- 3646 blue
- White 3747
Result control code:
- Code meaning
- -------------------------
- 0OFF
- 1 highlighted
- 4 underline
- 5 flashes
- 7. Reverse display
- 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:
- C_1 = "\ [\ e [0 m % \]"
- C0 = "\ [\ e [30 m % \]"
- C1 = "\ [\ e [31 m % \]"
- C2 = "\ [\ e [32 m % \]"
- C3 = "\ [\ e [33 m % \]"
- C4 = "\ [\ e [34 m % \]"
- C5 = "\ [\ e [35 m % \]"
- C6 = "\ [\ e [36 m % \]"
- C7 = "\ [\ e [37 m % \]"
- 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.