您可以使用以下ANSI 转义码 :
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
然后在脚本中像这样使用它们:
# .---------- constant part!
# vvvv vvvv-- the code from above
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"
用红色印出love
。
根据 @ james-lim 的注释, 如果使用的是echo
命令,请确保使用 - e 标志以允许反斜杠转义 。
# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow"
(除非要添加其他空行,否则在使用 echo 时请勿添加"\n"
)
您可以使用很棒的tput
命令(在Ignacio 的答案中建议)来为各种事物产生终端控制代码。
稍后将讨论特定的tput
子命令。
调用tput
作为命令序列的一部分:
tput setaf 1; echo "this is red text"
使用;
而不是&&
因此如果tput
错误仍然显示文本。
另一种选择是使用外壳变量:
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"
tput
产生的字符序列被终端解释为具有特殊含义。他们不会自己显示。请注意,它们仍然可以保存到文件中,也可以由终端以外的程序作为输入进行处理。
使用命令替换将tput
的输出直接插入echo
字符串中可能会更方便:
echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"
上面的命令在 Ubuntu 上生成:
tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape
颜色如下:
Num Colour #define R G B
0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 magenta COLOR_MAGENTA 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1
还有颜色设置功能的非 ANSI 版本( setb
代替setab
, setf
代替setaf
),它们使用不同的数字,此处未给出。
tput bold # Select bold mode
tput dim # Select dim (half-bright) mode
tput smul # Enable underline mode
tput rmul # Disable underline mode
tput rev # Turn on reverse video mode
tput smso # Enter standout (bold) mode
tput rmso # Exit standout mode
tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N # Move N characters forward (right)
tput cub N # Move N characters back (left)
tput cuu N # Move N lines up
tput ll # Move to last line, first column (if no cup)
tput sc # Save the cursor position
tput rc # Restore the cursor position
tput lines # Output the number of lines of the terminal
tput cols # Output the number of columns of the terminal
tput ech N # Erase N characters
tput clear # Clear screen and move the cursor to 0,0
tput el 1 # Clear to beginning of line
tput el # Clear to end of line
tput ed # Clear to end of screen
tput ich N # Insert N characters (moves rest of line forward!)
tput il N # Insert N lines
tput sgr0 # Reset text format to the terminal's default
tput bel # Play a bell
使用compiz摆动窗口时 , bel
命令会使终端摆动一秒钟,以引起用户的注意。
tput
接受每行包含一个命令的脚本,这些脚本按顺序在tput
退出之前执行。
通过回显多行字符串并将其用管道传递来避免使用临时文件:
echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red
man 1 tput
man 5 terminfo
。 (相应的tput
命令在Cap-name
81 行开始的巨大表的Cap-name
列中列出。) # Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
# High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White
| | bash | hex | octal | NOTE |
|-------+-------+--------+---------+------------------------------|
| start | \e | \x1b | \033 | |
| start | \E | \x1B | - | x cannot be capital |
| end | \e[0m | \x1m0m | \033[0m | |
| end | \e[m | \x1b[m | \033[m | 0 is appended if you omit it |
| | | | | |
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | \e[32m<text> | \x1b[32m<text> | \033[32m<text> | m is NOT optional |
| reset | <text>\e[0m | <text>\1xb[0m | <text>\033[om | o is optional (do it as best practice |
| | | | | |
如果您打算在特殊的 bash 变量中使用这些代码
您应该添加额外的转义字符,以便bash可以正确解释它们。如果没有添加额外的转义字符,它就可以工作,但是当您使用Ctrl + r
在历史记录中进行搜索时,您将遇到问题。
您应该在任何开始的 ANSI 代码之前添加\[
,在任何结束的 ANSI 代码之后添加\]
。
例:
正常使用: \033[32mThis is in green\033[0m
对于 PS0 / 1/2/4: \[\033[32m\]This is in green\[\033[m\]
\[
用于表示一系列不可打印的字符
\]
用于不可打印字符序列的结尾
提示:要记住,您可以先添加\[\]
,然后将 ANSI 代码放在它们之间:
- \[start-ANSI-code\]
- \[end-ANSI-code\]
在深入研究这些颜色之前,您应该了解以下代码的 4 种模式:
它修改了非文本颜色的样式。例如,使颜色变亮或变暗。
0
重置1;
比正常轻2;
比正常暗此模式不受广泛支持。它完全支持 Gnome 终端。
此模式用于修改文本的样式,而不是颜色。
3;
斜体4;
强调5;
闪烁(慢) 6;
闪烁(快速) 7;
相反8;
隐藏9;
划掉并且几乎得到支持。
例如,KDE-Konsole 支持5;
但是 Gnome 终端不支持,而 Gnome 支持8;
但是 KDE 不会。
此模式用于给前景着色。
此模式用于给背景着色。
下表总结了 ANSI-color 的3/4 位版本
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal | hex | bash | description | example (= in octal) | NOTE |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 0 | \033[0m | \x1b[0m | \e[0m | reset any affect | echo -e "\033[0m" | 0m equals to m |
| 1 | \033[1m | | | light (= bright) | echo -e "\033[1m####\033[m" | - |
| 2 | \033[2m | | | dark (= fade) | echo -e "\033[2m####\033[m" | - |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| text-mode | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 3 | \033[3m | | | italic | echo -e "\033[3m####\033[m" | |
| 4 | \033[4m | | | underline | echo -e "\033[4m####\033[m" | |
| 5 | \033[5m | | | blink (slow) | echo -e "\033[3m####\033[m" | |
| 6 | \033[6m | | | blink (fast) | ? | not wildly support |
| 7 | \003[7m | | | reverse | echo -e "\033[7m####\033[m" | it affects the background/foreground |
| 8 | \033[8m | | | hide | echo -e "\033[8m####\033[m" | it affects the background/foreground |
| 9 | \033[9m | | | cross | echo -e "\033[9m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| foreground | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 30 | \033[30m | | | black | echo -e "\033[30m####\033[m" | |
| 31 | \033[31m | | | red | echo -e "\033[31m####\033[m" | |
| 32 | \033[32m | | | green | echo -e "\033[32m####\033[m" | |
| 33 | \033[32m | | | yellow | echo -e "\033[33m####\033[m" | |
| 34 | \033[32m | | | blue | echo -e "\033[34m####\033[m" | |
| 35 | \033[32m | | | purple | echo -e "\033[35m####\033[m" | real name: magenta = reddish-purple |
| 36 | \033[32m | | | cyan | echo -e "\033[36m####\033[m" | |
| 37 | \033[32m | | | white | echo -e "\033[37m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 38 | 8/24 | This is for special use of 8-bit or 24-bit |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| background | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 40 | \033[40m | | | black | echo -e "\033[40m####\033[m" | |
| 41 | \033[41m | | | red | echo -e "\033[41m####\033[m" | |
| 42 | \033[42m | | | green | echo -e "\033[42m####\033[m" | |
| 43 | \033[43m | | | yellow | echo -e "\033[43m####\033[m" | |
| 44 | \033[44m | | | blue | echo -e "\033[44m####\033[m" | |
| 45 | \033[45m | | | purple | echo -e "\033[45m####\033[m" | real name: magenta = reddish-purple |
| 46 | \033[46m | | | cyan | echo -e "\033[46m####\033[m" | |
| 47 | \033[47m | | | white | echo -e "\033[47m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 48 | 8/24 | This is for special use of 8-bit or 24-bit | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
下表总结了 ANSI-color 的8 位版本
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | \033[38;5 | \x1b[38;5 | \e[38;5 | standard. normal | echo -e '\033[38;5;1m####\033[m' | |
| 8-15 | | | | standard. light | echo -e '\033[38;5;9m####\033[m' | |
| 16-231 | | | | more resolution | echo -e '\033[38;5;45m####\033[m' | has no specific pattern |
| 232-255 | | | | | echo -e '\033[38;5;242m####\033[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | | | | standard. normal | echo -e '\033[48;5;1m####\033[m' | |
| 8-15 | | | | standard. light | echo -e '\033[48;5;9m####\033[m' | |
| 16-231 | | | | more resolution | echo -e '\033[48;5;45m####\033[m' | |
| 232-255 | | | | | echo -e '\033[48;5;242m####\033[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
8 位快速测试:
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done
下表总结了 ANSI-color 的24 位版本
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | R = red | echo -e '\033[38;2;255;0;02m####\033[m' | R=255, G=0, B=0 |
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | G = green | echo -e '\033[38;2;;0;255;02m####\033[m' | R=0, G=255, B=0 |
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | B = blue | echo -e '\033[38;2;0;0;2552m####\033[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | R = red | echo -e '\033[48;2;255;0;02m####\033[m' | R=255, G=0, B=0 |
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | G = green | echo -e '\033[48;2;;0;255;02m####\033[m' | R=0, G=255, B=0 |
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | B = blue | echo -e '\033[48;2;0;0;2552m####\033[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
.gif
前景 8 位摘要
.gif
背景 8 位摘要
blinking
是的你可以。我对bash , c , c ++ , d perl , python 有经验
我想不是。
3/4 位是,如果您使用gcc
编译代码
Win-7 上的一些屏幕截图
\033[
= 2,其他部分 1
任何有tty
解释器的地方
xterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
等。
例如,如果要使用 mysql 着色输出,可以使用Perl
#!/usr/bin/perl -n
print "\033[1m\033[31m$1\033[36m$2\033[32m$3\033[33m$4\033[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^\w])/g;
将此代码存储在文件名: pcc
(= Perl Colorize Character)中,然后将文件 a 放入有效的PATH
然后在您喜欢的任何地方使用它。
ls | pcc
df | pcc
在mysql
内部,首先为pager
注册它,然后尝试:
[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;
它不处理 Unicode 的。
不,他们可以做很多有趣的事情。尝试:
echo -e '\033[2K' # clear the screen and do not move the position
要么:
echo -e '\033[2J\033[u' # clear the screen and reset the position
有很多初学者想用system( "clear" )
来清除屏幕,因此您可以使用它代替system(3)
调用
是。 \u001b
使用3/4-bit
很容易,但是使用24-bit
则非常准确和美观。
如果您没有使用html 的经验,那么这里有一个快速教程:
24 位表示: 00000000
和00000000
和00000000
。每个 8 位用于特定颜色。
1..8
用于和
9..16
用于和
17..24
为
所以在html 中 #FF0000
表示这是:
255;0;0
在html #00FF00
表示这是:
0;255;0
那有意义吗?您想要什么颜色将其与这三个 8 位值组合在一起。
参考:
维基百科
ANSI 转义序列
tldp.org
tldp.org
misc.flogisoft.com
我不记得的一些博客 / 网页
使用具有setaf
功能和参数1
tput
。
echo "$(tput setaf 1)Hello, world$(tput sgr0)"
echo -e "\033[31m Hello World"
[31m
控制文本颜色:
30
- 37
套前景色 40
- 47
套背景颜色可以在此处找到更完整的颜色代码列表。
优良作法是在字符串末尾将文本颜色重置回\033[0m
。
我刚刚合并了所有解决方案中的好方法,最后得出:
cecho(){
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
# ... ADD MORE COLORS
NC="\033[0m" # No Color
printf "${!1}${2} ${NC}\n"
}
您可以将其称为:
cecho "RED" "Helloworld"
这是颜色开关 \033[
。查看历史 。
颜色代码例如1;32
(浅绿色), 0;34
(蓝色), 1;34
(浅蓝色)等。
我们用彩色开关终止颜色序列\033[
和0m
, 无 -color 代码。就像以标记语言打开和关闭选项卡一样。
SWITCH="\033["
NORMAL="${SWITCH}0m"
YELLOW="${SWITCH}1;33m"
echo "${YELLOW}hello, yellow${NORMAL}"
简单的颜色echo
功能解决方案:
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo "$text"
}
cecho "Normal"
cecho y "Yellow!"
一种仅对一个echo
更改颜色的巧妙方法是定义以下功能:
function coloredEcho(){
local exp=$1;
local color=$2;
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput setaf $color;
echo $exp;
tput sgr0;
}
用法:
coloredEcho "This text is green" green
或者,您可以直接使用Drew 的答案中提到的颜色代码:
coloredEcho "This text is green" 2
使用tput
计算颜色代码。避免使用 ANSI 转义码(例如\E[31;1m
表示红色),因为它不那么可移植。例如,OS X 上的 Bash 不支持它。
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
BOLD=`tput bold`
RESET=`tput sgr0`
echo -e "hello ${RED}some red text${RESET} world"
这个问题已经被一遍又一遍地回答:-),但是为什么不呢。
第一次使用tput
在现代环境中比通过echo -E
手动注入 ASCII 代码更容易携带
这是一个快速的 bash 函数:
say() {
echo "$@" | sed \
-e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \
-e "s/@red/$(tput setaf 1)/g" \
-e "s/@green/$(tput setaf 2)/g" \
-e "s/@yellow/$(tput setaf 3)/g" \
-e "s/@blue/$(tput setaf 4)/g" \
-e "s/@magenta/$(tput setaf 5)/g" \
-e "s/@cyan/$(tput setaf 6)/g" \
-e "s/@white/$(tput setaf 7)/g" \
-e "s/@reset/$(tput sgr0)/g" \
-e "s/@b/$(tput bold)/g" \
-e "s/@u/$(tput sgr 0 1)/g"
}
现在您可以使用:
say @b@green[[Success]]
要得到:
tput
可移植性的注意事项 1986 年 9 月首次上载tput(1)
源代码
tput(1)
在 1990 年代的 X / Open curses 语义中可用(1997 标准具有以下提及的语义)。
因此,它是( 相当 )普遍。