日期:2014-05-16  浏览次数:20686 次

Linux中的颜色方案

在linux下可以自定义自己的颜色方案,不管是linux命令提示符的颜色,还是stdout的输出颜色。

/etc/DIR_COLORS 下可以找到如下说明:

?

[root@CentOS ~]
#cat /etc/DIR_COLORS 

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes: 
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00       # global default, although everything should be something.
FILE 00         # normal file
DIR 01;34       # directory
LINK 01;36      # symbolic link
FIFO 40;33      # pipe
SOCK 01;35      # socket
BLK 40;33;01    # block device driver
CHR 40;33;01    # character device driver
ORPHAN 01;05;37;41  # orphaned syminks
MISSING 01;05;37;41 # ... and the files they point to

?

?

--最重要的部分在这里:

?

# Attribute codes: 
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

?

?

下面 举两个例子:

?

1、让输出的字符带上颜色

定义一个脚本:

?

#!/bin/bash
# 先定义一些颜色:
red='\e[0;31m'          # 红色
RED='\e[1;31m'          # 红色+粗体(后面以此类推)
green='\e[0;32m'        # 绿色
GREEN='\e[1;32m'
yellow='\e[0;33m'       # 黄色
YELLOW='\e[1;33m'
blue='\e[0;34m'         # 蓝色
BLUE='\e[1;34m'
purple='\e[0;35m'       # 紫色
PURPLE='\e[1;35m'
cyan='\e[0;36m'         # 蓝绿色
CYAN='\e[1;36m'
WHITE='\e[1;37m'        # 白色
NC='\e[0m' # 没有颜色

echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"
echo -e "${RED}RED ${BLUE}BLUE ${cyan}cyan ${GREEN}GREEN${NC}"
echo -e "${CYAN}white ${WHITE}blod white ${NC} no color!!"
echo "${CYAN}white ${WHITE}blod white ${NC} no color!!"    #注意这句

?

运行结果如下:


?

注:这里要加上 -e 参数才能正确输出颜色。

( -e 的解释:-e ? ? enable interpretation of backslash escapes | 大致意思是允许解释反斜杠)



2、修改命令提示符(也就是:[root@CentOS ~]#?)
我们可以给?[root@CentOS ~]# ?加上颜色和自定义格式:

修改 /etc/bashrc 来达成我们的目的:
vi /ete/bashrc,跟上面一样,大概修改成下面的样子(颜色可以自己换)
        red='\e[0;31m'          # 红色
        cyan='\e[0;36m'         # 蓝绿色
        NC='\e[0m' # 没有颜色
    # [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[${red}\u${NC}@${cyan}\h${NC} \W]\\$ "
?
然后保存退出,重新登录终端,输出结果如下:


?
颜色已经改变,不在是默认的全白色啦~


关于命令提示符的格式,请参考这个:
\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