您的位置:首页 > 其它

修改终端命令提示符颜色

2016-01-11 21:05 447 查看
转载:http://www.centoscn.com/CentOS/2013/0418/321.html

相信很多人已经看厌了Linux已成不变的命令提示符的颜色,多数人要么使用默认的绿色,要么在使用PUTTY的时候设置成绿色的,不知道是否有人想到提示符可以设置成其他的颜色呢,本文就说明命令提示符变量PS1的设置。


1、PS1变量简介

PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置。

可以使用 man bash命令查看bash手册,找到该变量支持的特殊字符,以及这些特殊字符的意义:

 

\a     #an ASCII bell character (07)   

\d     #the date in "Weekday Month Date" format (e.g.,  "Tue  May   

       26")   

\D{format}
  

       #the  format  is  passed  to strftime(3) and the result is   

       #inserted into the prompt string; an empty format  results   

       #in a locale-specific time representation.  The braces are   

       #required   

\e     #an ASCII escape character (033)   

\h     #the hostname up to the first ‘.’   

  \H     #the hostname   

\j     #the number of jobs currently managed by the shell   

\l     #the basename of the shell’s terminal device name   

\n     #newline   

\r     #carriage return   

\s     #the name of the shell, the basename of  $0  (the  portion   

       #following the final slash)   

\t     #the current time in 24-hour HH:MM:SS format   

\T     #the current time in 12-hour HH:MM:SS format   

\@     #the current time in 12-hour am/pm format   

\A     #the current time in 24-hour HH:MM format   

\u     #the username of the current user   

\v     #the version of bash (e.g., 2.00)   

\V     #the release of bash, version + patch level (e.g., 2.00.0)   

\w     #the current working  directory,  with  $HOME  abbreviated   

       #with  a tilde (uses the value of the PROMPT_DIRTRIM vari-   

       #able)   

\W     #the basename of the current working directory, with $HOME   

       #abbreviated with a tilde   

\!     #the history number of this command   

\#     #the command number of this command   

\$     #if the effective UID is 0, a #, otherwise a $   

\nnn   #the character corresponding to the octal number nnn   

\\     #a backslash   

\[     #begin  a sequence of non-printing characters, which could   

       #be used to embed a terminal  control  sequence  into  the   

       #prompt   

\]     #end a sequence of non-printing characters  

下面我把常用的特殊字符做中文解释:

 

\d :#代表日期,格式为weekday month date,例如:"Mon Aug 1"   

\H :#完整的主机名称。   

\h :#仅取主机的第一个名字,如上例,则为fc4,.linux则被省略   

\t :#显示时间为24小时格式,如:HH:MM:SS   

\T :#显示时间为12小时格式   

\A :#显示时间为24小时格式:HH:MM   

\u :#当前用户的账号名称   

\v :#BASH的版本信息   

\w :#完整的工作目录名称。家目录会以 ~代替   

\W :#利用basename取得工作目录名称,所以只会列出最后一个目录   

\# :#下达的第几个命令   

\$ :#提示字符,如果是root时,提示符为:# ,普通用户则为:$  


2、颜色设置说明

在PS1中设置字符序列颜色的格式为:\[\e[F;Bm\]

其中“F”为字体颜色,编号30~37;“B”为背景色,编号40~47。

下面看下颜色表:

 

    前景     背景      颜色

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

  30      40      黑色   

  31      41      红色   

  32      42      绿色   

  33      43      黄色   

  34      44      蓝色   

  35      45      紫红色   

  36      46      青蓝色   

  37      47      白色  

效果控制代码:

 

代码              意义   

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

 0                 OFF   

 1                 高亮显示   

 4                 underline   

 5                 闪烁   

 7                 反白显示   

 8                 不可见  


3、示例

我们用下面这个例子,解析PS1变量来设置有颜色的命令提示符:

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

说明:

‘\[\e[32m\]’用来设置‘#\##’的颜色为绿色,#\##就是显示现在运行的是第几条命令

‘[\e[31m\]’设置‘\u@’的颜色为红色并高亮显示,如果指定多个数字需要用分号隔开。\u@ 就是当前登录的用户名后跟一个‘@’符号。

‘\[\e[36m\]’设置‘\h\w’为青蓝色,\h表示主机名的第一位,如果主机名为centos6.lampbo.org,那么就显示centos6;\w将显示完整的绝对路径。

‘\$’ 提示字符,如果是root时,提示符为:# ,普通用户则为:$。

‘\[\e[m\]’使用来关闭颜色设置的。要是你没有这个的话;那么,你的命令提示符,包括你通过命令提示符输出的东西都是和最后一次的颜色设置相同。

为了能够在启动和登录是可以保持刚刚设置的变量,需要将PS1的设置加入到用户home目录的.bashrc文件后。

额外的示例:

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

(2):编辑.bashrc,加入以下内容:

 

c_1="\[\e[0m%\]"  

c0="\[\e[30m%\]"  

c1="\[\e[31m%\]"  

c2="\[\e[32m%\]"  

c3="\[\e[33m%\]"  

c4="\[\e[34m%\]"  

c5="\[\e[35m%\]"  

c6="\[\e[36m%\]"  

c7="\[\e[37m%\]"  

PS1="$c0***** $c1\w $c2*** $c3<\u@\h> $c4***** $c5\! $c6***** $c7\t $c1***\n$c2\$ $c_1"; export PS1
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  终端