長期對着黑白的終端,可能會覺得枯燥。其實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顯示: