发布时间:2014-09-05 16:52:00作者:知识屋
在linux下可以自定义自己的颜色方案,不管是linux命令提示符的颜色,还是stdout的输出颜色。
在 /etc/DIR_COLORS 下可以找到如下说明:
Shell代码
[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
--最重要的部分在这里:
Txt代码
# 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、让输出的字符带上颜色
定义一个脚本:
Shell代码
#!/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,跟上面一样,大概修改成下面的样子(颜色可以自己换)
Shell代码
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]//$ "
然后保存退出,重新登录终端,输出结果如下:
颜色已经改变,不在是默认的全白色啦~
关于命令提示符的格式,请参考这个:
Txt代码
/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
最好的办法就是一次全部打印出来试试,嘿嘿。
---------------------------------------------------------华丽的结束符------------------------------------------------
希望对你也有所帮助,tks……
作者“成长的日子”
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能