shell 颜色控制

shell 颜色控制
2008-06-27 12:53

Q. How do I change the color of my shell prompt under Linux ?

A. You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt.

In the Linux default shell is BASH.

Your current prompt setting is stored in PS1 shell variable. There are other variables too, like PS2, PS3 and PS4.

Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Task: Display current BASH prompt (PS1)

Use echo command to display current BASH prompt:
$ echo $PS1Output:

[\u@h \W]\$

By default the command prompt is set to: [\u@\h \W]\$. Backslash-escaped special characters are decoded as follows:

  • \u: Display the current username
  • \h: Display the hostname
  • \W: Print the current working directory

Task: Modify current BASH prompt

Use export command to setup a new shell prompt:$ export PS1="[\\u@\\H \\W \\@]\\$"

Where,

  • \H: Display FQDN hostname
  • \@: Display current time in 12-hour am/pm format

Task: Add colors to the prompt

To add colors to the shell prompt use the following export command syntax:
‘\e[x;ym $PS1 \e[m’

Where,

  • \e[ Start color scheme
  • x;y Color pair to use (x;y)
  • $PS1 is your shell prompt
  • \e[m Stop color scheme

To set a red color prompt, type the command:
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

List of Color code

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33

Replace digit 0 with 1 to get light color version.

Task: How to make the prompt setting permanent

Your new shell prompt setting is temporary i.e. when you logout setting will be lost. To have it set everytime you login to your workstation add above export command to your .bash_profile file or .bashrc file.
$ cd
$ vi .bash_profile
OR
$ vi .bashrc
Append export line:
export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

Save and close the file.

tput command

You can also use tput command. For example display RED prompt use tput as follows:
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

handy tput commands

  • tput bold - Bold effect
  • tput rev - Display inverse colors
  • tput sgr0 - Reset everything
  • tput setaf {CODE}- Set foreground color, see color {CODE} below
  • tput setab {CODE}- Set background color, see color {CODE} below

Colors {code} code for tput command

Color {code} Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White

Read the man page of bash and tput command for more information.

ANSI控制码的说明
例如:
   echo -ne "\33[32m" 可以将字符的显示颜色改为绿色
   echo -ne "\33[3;1H" 可以将光标移到第3行第1列处
具体的摘抄一些如下:
\33[0m   关闭所有属性
\33[1m   设置高亮度
\33[4m   下划线
\33[5m   闪烁
\33[7m   反显
\33[8m   消隐
\33[30m -- \33[37m   设置前景色
\33[40m -- \33[47m   设置背景色
\33[nA   光标上移n行
\33[nB   光标下移n行
\33[nC   光标右移n行
\33[nD   光标左移n行
\33[y;xH设置光标位置
\33[2J   清屏
\33[K    清除从光标到行尾的内容
\33[s    保存光标位置
\33[u    恢复光标位置
\33[?25l   隐藏光标
\33[?25h   显示光标
所有的转移字符表:
\a ASCII 响铃字符(也可以键入 \007)
\d "Wed Sep 06" 格式的日期
\e ASCII 转义字符(也可以键入 \033)
\h 主机名的第一部分(如 "mybox")
\H 主机的全称(如 "mybox.mydomain.com")
\j 在此 shell 中通过按 ^Z 挂起的进程数
\l 此 shell 的终端设备名(如 "ttyp4")
\n 换行符
\r 回车符
\s shell 的名称(如 "bash")
\t 24 小时制时间(如 "23:01:01")
\T 12 小时制时间(如 "11:01:01")
\@ 带有 am/pm 的 12 小时制时间
\u 用户名
\v bash 的版本(如 2.04)
\V Bash 版本(包括补丁级别) ?/td>
\w 当前工作目录(如 "/home/drobbins")
\W 当前工作目录的“基名 (basename)”(如 "drobbins")
\! 当前命令在历史缓冲区中的位置
\# 命令编号(只要您键入内容,它就会在每次提示时累加)
\$ 如果您不是超级用户 (root),则插入一个 "$";如果您是超级用户,则显示一个 "#"
\xxx 插入一个用三位数 xxx(用零代替未使用的数字,如 "\007")表示的 ASCII 字符
\\ 反斜杠
\[ 这个序列应该出现在不移动光标的字符序列(如颜色转义序列)之前。它使 bash 能够正确计算自动换行。
\] 这个序列应该出现在非打印字符序列之后。

这样,您已经知道了 bash 中用反斜杠转义的全部专用序列。请稍微演练一下这些序列,以对它们的工作方式获得一些感性认识。在您做了一些测试之后,下面开始添加颜色。

========================================
附上一个例子:
#!/bin/sh
############################################################
# Nico Golde <nico(at)ngolde.de> Homepage: http://www.ngolde.de
# Last change: Mon Feb 16 16:24:41 CET 2004
############################################################

for attr in 0 1 4 5 7 ; do
    echo "----------------------------------------------------------------"
    printf "ESC[%s;Foreground;Background - \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

猜你喜欢

转载自jarit.iteye.com/blog/1233237
今日推荐