我正在尝试使用echo命令在终端中打印文本。
我想以红色打印文本。我怎样才能做到这一点?
你可以使用这些ANSI escape codes:
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"
)
感谢@ k-five这个答案
tput(1)
tput(1)
希望这个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"
}
帮助你为你的bash挑选你的颜色:D
这些代码适用于我的Ubuntu盒子:
cecho "RED" "Helloworld"
这将以不同的颜色打印字母a b c d:
declare -A colors
#curl www.bunlongheng.com/code/colors.png
# Reset
colors[Color_Off]='\033[0m' # Text Reset
# Regular Colors
colors[Black]='\033[0;30m' # Black
colors[Red]='\033[0;31m' # Red
colors[Green]='\033[0;32m' # Green
colors[Yellow]='\033[0;33m' # Yellow
colors[Blue]='\033[0;34m' # Blue
colors[Purple]='\033[0;35m' # Purple
colors[Cyan]='\033[0;36m' # Cyan
colors[White]='\033[0;37m' # White
# Bold
colors[BBlack]='\033[1;30m' # Black
colors[BRed]='\033[1;31m' # Red
colors[BGreen]='\033[1;32m' # Green
colors[BYellow]='\033[1;33m' # Yellow
colors[BBlue]='\033[1;34m' # Blue
colors[BPurple]='\033[1;35m' # Purple
colors[BCyan]='\033[1;36m' # Cyan
colors[BWhite]='\033[1;37m' # White
# Underline
colors[UBlack]='\033[4;30m' # Black
colors[URed]='\033[4;31m' # Red
colors[UGreen]='\033[4;32m' # Green
colors[UYellow]='\033[4;33m' # Yellow
colors[UBlue]='\033[4;34m' # Blue
colors[UPurple]='\033[4;35m' # Purple
colors[UCyan]='\033[4;36m' # Cyan
colors[UWhite]='\033[4;37m' # White
# Background
colors[On_Black]='\033[40m' # Black
colors[On_Red]='\033[41m' # Red
colors[On_Green]='\033[42m' # Green
colors[On_Yellow]='\033[43m' # Yellow
colors[On_Blue]='\033[44m' # Blue
colors[On_Purple]='\033[45m' # Purple
colors[On_Cyan]='\033[46m' # Cyan
colors[On_White]='\033[47m' # White
# High Intensity
colors[IBlack]='\033[0;90m' # Black
colors[IRed]='\033[0;91m' # Red
colors[IGreen]='\033[0;92m' # Green
colors[IYellow]='\033[0;93m' # Yellow
colors[IBlue]='\033[0;94m' # Blue
colors[IPurple]='\033[0;95m' # Purple
colors[ICyan]='\033[0;96m' # Cyan
colors[IWhite]='\033[0;97m' # White
# Bold High Intensity
colors[BIBlack]='\033[1;90m' # Black
colors[BIRed]='\033[1;91m' # Red
colors[BIGreen]='\033[1;92m' # Green
colors[BIYellow]='\033[1;93m' # Yellow
colors[BIBlue]='\033[1;94m' # Blue
colors[BIPurple]='\033[1;95m' # Purple
colors[BICyan]='\033[1;96m' # Cyan
colors[BIWhite]='\033[1;97m' # White
# High Intensity backgrounds
colors[On_IBlack]='\033[0;100m' # Black
colors[On_IRed]='\033[0;101m' # Red
colors[On_IGreen]='\033[0;102m' # Green
colors[On_IYellow]='\033[0;103m' # Yellow
colors[On_IBlue]='\033[0;104m' # Blue
colors[On_IPurple]='\033[0;105m' # Purple
colors[On_ICyan]='\033[0;106m' # Cyan
colors[On_IWhite]='\033[0;107m' # White
color=${colors[$input_color]}
white=${colors[White]}
# echo $white
for i in "${!colors[@]}"
do
echo -e "$i = ${colors[$i]}I love you$white"
done
对于循环:
如果你想提高代码的可读性,你可以首先使用image字符串,然后使用echo -e "\x1B[31m foobar \x1B[0m"
echo -e "\x1B[32m foobar \x1B[0m"
echo -e "\x1B[96m foobar \x1B[0m"
echo -e "\x1B[01;96m foobar \x1B[0m"
echo -e "\x1B[01;95m foobar \x1B[0m"
echo -e "\x1B[01;94m foobar \x1B[0m"
echo -e "\x1B[01;93m foobar \x1B[0m"
echo -e "\x1B[01;91m foobar \x1B[0m"
echo -e "\x1B[01;90m foobar \x1B[0m"
echo -e "\x1B[01;89m foobar \x1B[0m"
echo -e "\x1B[01;36m foobar \x1B[0m"
添加颜色:
echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"
到目前为止,我最喜欢的答案是有色的Echo。
只是发布另一个选项,你可以查看这个小工具xcol
for (( i = 0; i < 17; i++ ));
do echo "$(tput setaf $i)This is ($i) $(tput sgr0)";
done
你就像使用grep一样使用它,例如,它会为每个参数用不同的颜色着色它的标准输入
echo
sed
请注意,它接受sed将接受的任何正则表达式。
此工具使用以下定义
echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'
我在我的脚本中使用这些变量
https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/
没有人注意到ANSI代码7反转视频的有用性。
通过交换前景色和背景色,它可以在任何终端方案颜色,黑色或白色背景或其他幻想调色板上保持可读性。
例如,对于无处不在的红色背景:
#normal=$(tput sgr0) # normal text
normal=$'\e[0m' # (works better sometimes)
bold=$(tput bold) # make colors bold/bright
red="$bold$(tput setaf 1)" # bright red text
green=$(tput setaf 2) # dim green text
fawn=$(tput setaf 3); beige="$fawn" # dark yellow text
yellow="$bold$fawn" # bright yellow text
darkblue=$(tput setaf 4) # dim blue text
blue="$bold$darkblue" # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple" # bright magenta text
darkcyan=$(tput setaf 6) # dim cyan text
cyan="$bold$darkcyan" # bright cyan text
gray=$(tput setaf 7) # dim white text
darkgray="$bold"$(tput setaf 0) # bold black = dark gray text
white="$bold$gray" # bright white text
这是改变终端内置方案时的外观:
echo "${red}hello ${yellow}this is ${green}coloured${normal}"
这是用于gif的循环脚本。
this answer
见function echocolor() { # $1 = string
COLOR='\033[1;33m'
NC='\033[0m'
printf "${COLOR}$1${NC}\n"
}
echo "This won't be colored"
echocolor "This will be colorful"
你绝对应该使用tput而不是原始的ANSI控制序列。
因为存在大量不同的终端控制语言,所以通常系统具有中间通信层。在数据库中查找实际代码以获取当前检测到的终端类型,并将标准化请求提供给API或(从shell)到命令。
其中一个命令是
echo -e "\033[31;7mHello world\e[0m";
。 接受一组称为能力名称和任何参数的首字母缩略词,如果合适,然后在terminfo数据库中查找检测到的终端的正确转义序列并打印正确的代码(终端希望了解)。
来自for i in {30..49};do echo -e "\033[$i;7mReversed color code $i\e[0m Hello world!";done
也就是说,我写了一个名为https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters的小助手库,它在tput之上添加了另一层,使其更易于使用(imho):
示例:tput
会得到以下结果:tput
我写过http://wiki.bash-hackers.org/scripting/terminalcodes来实现这一目标。
你可以这样做
bash-tint
现在,您可以通过以下方式将所有转义命令作为txt文件安装到给定目标:
tint "white(Cyan(T)Magenta(I)Yellow(N)Black(T)) is bold(really) easy to use."
甚至更容易通过:
哪个会将颜色安装到swag。
要么像这样使用它们:
pip install swag
或者这样,我发现其实更有趣:
swag install -d <colorsdir>
在swag install
上查看!
这就是我以前看到的所有组合并决定哪些内容很酷:
~/.colors
您可以使用awesome tput
命令(在Ignacio's answer中建议)为各种事物生成终端控制代码。
具体的tput
子命令将在后面讨论。
将tput
作为命令序列的一部分调用:
tput setaf 1; echo "this is red text"
使用;
而不是&&
所以如果tput
错误文本仍然显示。
另一种选择是使用shell变量:
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"
tput
生成由终端解释为具有特殊含义的字符序列。他们不会自己出现。请注意,它们仍然可以保存到文件中或由终端以外的程序作为输入处理。
使用tput
将echo
的输出直接插入command substitution字符串可能更方便:
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 wobbly windows,bel
命令使终端摆动一秒钟以引起用户的注意。
tput
接受每行包含一个命令的脚本,这些脚本在tput
退出之前按顺序执行。
通过回显多行字符串并管道来避免临时文件:
echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red
man 1 tput
man 5 terminfo
。 (相应的tput
命令列在从第81行开始的巨大表格的Cap-name
列中。)这是一个简单的小脚本,我最近放在一起,它将使任何管道输入着色而不是使用“厕所”。
echo $(cat ~/.colors/blue.txt) This will be blue
swag print -c red -t underline "I will turn red and be underlined"
然后用红色调用它(196): asciinema
参考:
for (( i = 0; i < 8; i++ )); do
for (( j = 0; j < 8; j++ )); do
printf "$(tput setab $i)$(tput setaf $j)(b=$i, f=$j)$(tput sgr0)\n"
done
done
这是最简单易读的解决方案。使用bashj(File: color.bsh
),您只需选择以下行之一:
#!/usr/bin/env bash
## A.M.Danischewski 2015+(c) Free - for (all (uses and
## modifications)) - except you must keep this notice intact.
declare INPUT_TXT=""
declare ADD_LF="\n"
declare -i DONE=0
declare -r COLOR_NUMBER="${1:-247}"
declare -r ASCII_FG="\\033[38;05;"
declare -r COLOR_OUT="${ASCII_FG}${COLOR_NUMBER}m"
function show_colors() {
## perhaps will add bg 48 to first loop eventually
for fgbg in 38; do for color in {0..256} ; do
echo -en "\\033[${fgbg};5;${color}m ${color}\t\\033[0m";
(($((${color}+1))%10==0)) && echo; done; echo; done
}
if [[ ! $# -eq 1 || ${1} =~ ^-. ]]; then
show_colors
echo " Usage: ${0##*/} <color fg>"
echo " E.g. echo \"Hello world!\" | figlet | ${0##*/} 54"
else
while IFS= read -r PIPED_INPUT || { DONE=1; ADD_LF=""; }; do
PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}")
INPUT_TXT="${INPUT_TXT}${PIPED_INPUT}${ADD_LF}"
((${DONE})) && break;
done
echo -en "${COLOR_OUT}${INPUT_TXT}\\033[00m"
fi
如果您的终端应用程序中有颜色支持,则可以使用$> echo "text you want colored red" | color.bsh 196
颜色。
就像那里的东西一样,将它传递给grep会将其突出显示为红色(但仅为红色)。您还可以使用命名管道,以便您的字符串更接近行尾:
echo_red(){
echo -e "\e[1;31m$1\e[0m"
}
echo_green(){
echo -e "\e[1;32m$1\e[0m"
}
echo_yellow(){
echo -e "\e[1;33m$1\e[0m"
}
echo_blue(){
echo -e "\e[1;34m$1\e[0m"
}
https://sourceforge.net/projects/bashj/
这个答案是正确的,除了对颜色的调用不应该在引号内。
#!/usr/bin/bash
W="Hello world!"
echo $W
R=130
G=60
B=190
echo u.colored($R,$G,$B,$W)
echo u.colored(255,127,0,$W)
echo u.red($W)
echo u.bold($W)
echo u.italic($W)
Y=u.yellow($W)
echo $Y
echo u.bold($Y)
应该做的伎俩。
# 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代码之前添加\[
,并在任何结束之后添加\]
。
例:
经常使用:\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-Terminal。
此模式用于修改文本样式而不是颜色。
3;
斜体4;
强调5;
闪烁(慢)6;
闪烁(快)7;
逆转8;
隐藏9;
横空出世并几乎得到支持。
例如,KDE-Konsole支持5;
,但Gnome-Terminal不支持,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 | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
下表显示了8位版本的ANSI颜色的摘要
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 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
下表显示了24位版本的ANSI颜色的摘要
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 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
on KDE-Terminal是的你可以。我在bash,c,c++,d perl,python经历过
我想不是。
3/4位是的,如果你用gcc
编译代码
some screen-shots on 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),然后将文件放入有效的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位值结合起来。
参考: Wikipedia ANSI escape sequences tldp.org tldp.org misc.flogisoft.com 一些我不记得的博客/网页
使用tput
与setaf
能力和1
参数。
echo "$(tput setaf 1)Hello, world$(tput sgr0)"
echo -e "\033[31m Hello World"
[31m
控制文本颜色:
一个更完整的颜色代码列表30
。
最好将文本颜色重置为字符串末尾的37
。
这是颜色开关40
。见47
。
颜色can be found here像\033[0m
(浅绿色),\033[
(蓝色),history(浅蓝色)等。
我们使用颜色开关codes和1;32
(无颜色代码)终止颜色序列。就像用标记语言打开和关闭标签一样。
0;34
简单的颜色1;34
功能解决方案:
\033[
只为一个0m
改变颜色的巧妙方法是定义这样的功能:
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;
}
计算颜色代码。避免使用ANSI转义码(例如coloredEcho "This text is green" green
表示红色),因为它的可移植性较差。例如,OS X上的Bash不支持它。
Drew's answer
这个问题一遍又一遍地回答:-)但为什么不呢。
首先使用coloredEcho "This text is green" 2
在现代环境中比通过tput
手动注入ASCII代码更容易移植
这是一个快速bash功能:
\E[31;1m
现在你可以使用:
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
第一次 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"
}
源代码于1986年9月上传
say @b@green[[Success]]
在20世纪90年代已经在X / Open curses语义中可用(1997年标准具有下面提到的语义)。
所以,它(非常)无所不在。