Linux终端颜色设置 (Bash Color Setting)

长期对着黑白的终端,可能会觉得枯燥。其实Bash允许自定义彩色的命令提示符、彩色的grep显示、彩色的man显示、彩色的ls显示等等。

 

我们只需要编辑个人或者全局的shell配置文件就可以构建自己的独特的多姿多彩的shell。其中,用户个人配置文件是~/.bashrc,全局配置文件是/etc/bash.bashrc(ubuntu)或者/etc/bashrc(Fedora)。

 

 


彩色的命令提示符

 

在配置文件中设置环境变量PS1,如下: (注意,除了待显示文本,千万不要有多余的空格)


export PS1="/[/033[1;32m/] [ /[/033[1;31m/] /u@/h: /[/033[1;34m/] /w /[/033[1;32m/] ] /[/033[1;31m/] /$ /[/033[0m/] "

 

PS1的内容由若干个如下片段组成(除了最后一个片段):


/[ 颜色描述字符串/] 待显示文本

最后一个片段没有待显式文本,待显示文本就是用户输入的命令。这里用的颜色 /[/033[0m/] ,是指默认值。/[和/]是转义符,其内部是非打印字符,有时候可以不要这两个转义符。

 

颜色描述字符串的格式为:


/033[特殊格式;字体颜色;背景颜色m

其中/033是键盘上Esc键对应的ASCII码(27=/033=0x1B),指示:后面的内容是特殊含义的,等效写法有 ^[ 以及 /e 。特殊格式、字体颜色、背景颜色可以省略,其顺序也无所谓,只要中间用;隔开即可。下面的表格列举了各个成员的可能取值及其含义,可以看到各成员的取值两类不同。

特殊格式 含义 字体颜色值 背景颜色值 颜色
 0  默认值

30

 40  黑色
 1  粗体  31  41  红色
 22  非粗体  32  42  绿色
 4  下划线  33  43  黄色
24  非下划线  34  44  蓝色
 5   闪烁  35  45  洋红
 25  非闪烁  36  46  青色
 7  反显  37  47  白色
 27  非反显    

 

 

 

值得注意的是,的含义在终端中是可以调整的 。一般终端的菜单中有一个color palette(比如SecureCRT和GNOME Terminal都有)。一般是两行,每行8种颜色。我们可以更改每种颜色。这两行颜色分别对应了/e[0;30m ~ /e[0;37m 以及 /e[1;30m ~ /e[1;30m 的颜色。

 

转义符:


  /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
  /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
 

 

 

测试脚本:
#!/bin/sh
for attr in 0 1 4 5 7; do
printf "/033[1;31m//e[/033[0m%s/033[1;31m;/033[0mForground/033[1;31m;/033[0mBackground/033[1;31mm /033[0m- /n" $attr
for fore in 30 31 32 33 34 35 36 37; do
for back in 40 41 42 43 44 45 46 47; do
printf '/033[%s;%s;%sm %02s;%02s    ' $attr $fore $back $fore $back
done
printf '/n'
done
printf '/033[0m'
done

 

参考:
http://wiki.archlinux.org/index.php/Color_Bash_Prompt
http://www.51testing.com/?uid-175444-action-viewspace-itemid-101030
 
彩色的man显示:

export PAGER="/usr/bin/less -s" export BROWSER="$PAGER" 
export LESS_TERMCAP_mb=$'/E[01;34m'
export LESS_TERMCAP_md=$'/E[01;34m'
export LESS_TERMCAP_me=$'/E[0m'
export LESS_TERMCAP_se=$'/E[0m'
export LESS_TERMCAP_so=$'/E[01;44;33m'
export LESS_TERMCAP_ue=$'/E[0m'
export LESS_TERMCAP_us=$'/E[01;33m' 

彩色的grep显示: 

alias grep='grep --colour=auto'
 

 

版权声明:本文为转载文章,格式稍有调整,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

 
 

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章