如何在 Linux 中更改 echo 的输出颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5947742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:53:02  来源:igfitidea点击:

How to change the output color of echo in Linux

linuxbashcommand-lineechoterminal-color

提问by satheesh.droid

I am trying to print a text in the terminal using echo command.

我正在尝试使用 echo 命令在终端中打印文本。

I want to print the text in a red color. How can I do that?

我想以红色打印文本。我怎样才能做到这一点?

回答by neocanable

echo -e "3[31m Hello World"

The [31mcontrols the text color:

[31m控制文本颜色:

  • 30-37sets foregroundcolor
  • 40-47sets backgroundcolor
  • 30-37前景颜色
  • 40-47设置背景颜色

A more complete list of color codes can be found here.

可以在此处找到更完整的颜色代码列表。

It is good practice to reset the text color back to \033[0mat the end of the string.

将文本颜色重置回\033[0m字符串末尾是一种很好的做法。

回答by Ignacio Vazquez-Abrams

Use tputwith the setafcapability and a parameter of 1.

使用tputsetaf能力的参数1

echo "$(tput setaf 1)Hello, world$(tput sgr0)"

回答by Tobias

You can use these ANSI escape codes:

您可以使用这些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

And then use them like this in your script:

然后在脚本中像这样使用它们:

#    .---------- constant part!
#    vvvv vvvv-- the code from above
RED='3[0;31m'
NC='3[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"

which prints lovein red.

love以红色打印。

From @james-lim's comment, if you are using the echocommand, be sure to use the -e flag to allow backslash escapes.

根据@james-lim 的评论,如果您正在使用该echo命令,请务必使用 -e 标志以允许反斜杠转义

# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow"

(don't add "\n"when using echo unless you want to add additional empty line)

"\n"除非您想添加额外的空行,否则在使用 echo 时不要添加)

回答by Drew Noakes

You can use the awesome tputcommand (suggested in Ignacio's answer) to produce terminal control codes for all kinds of things.

您可以使用 awesometput命令(在Ignacio's answer 中建议)为各种事物生成终端控制代码。



Usage

用法

Specific tputsub-commands are discussed later.

具体的tput子命令稍后讨论。

Direct

直接的

Call tputas part of a sequence of commands:

tput作为命令序列的一部分调用:

tput setaf 1; echo "this is red text"

Use ;instead of &&so if tputerrors the text still shows.

如果文本仍然显示错误,请使用;而不是&&so tput

Shell variables

外壳变量

Another option is to use shell variables:

另一种选择是使用 shell 变量:

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"

tputproduces character sequences that are interpreted by the terminal as having a special meaning. They will not be shown themselves. Note that they can still be saved into files or processed as input by programs other than the terminal.

tput产生由终端解释为具有特殊含义的字符序列。他们不会被展示出来。请注意,它们仍然可以保存到文件中或由终端以外的程序作为输入处理。

Command substitution

命令替换

It may be more convenient to insert tput's output directly into your echostrings using command substitution:

使用命令替换tput的输出直接插入echo字符串可能更方便:

echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"


Example

例子

The above command produces this on Ubuntu:

上面的命令在 Ubuntu 上生成:

Screenshot of colour terminal text

彩色终端文本截图



Foreground & background colour commands

前景色和背景色命令

tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape

Colours are as follows:

颜色如下:

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

There are also non-ANSI versions of the colour setting functions (setbinstead of setab, and setfinstead of setaf) which use different numbers, not given here.

也有非 ANSI 版本的颜色设置函数(setb代替setabsetf代替setaf)使用不同的数字,这里没有给出。

Text mode commands

文本模式命令

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

Cursor movement commands

光标移动命令

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

Clear and insert commands

清除和插入命令

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

Other commands

其他命令

tput sgr0    # Reset text format to the terminal's default
tput bel     # Play a bell

With compiz wobbly windows, the belcommand makes the terminal wobble for a second to draw the user's attention.

使用compiz wobbly windows,该bel命令使终端摆动一秒钟以引起用户的注意。



Scripts

脚本

tputaccepts scripts containing one command per line, which are executed in order before tputexits.

tput接受每行包含一个命令的脚本,这些脚本在tput退出前按顺序执行。

Avoid temporary files by echoing a multiline string and piping it:

通过回显多行字符串并通过管道传输来避免临时文件:

echo -e "setf 7\nsetb 1" | tput -S  # set fg white and bg red


See also

也可以看看

  • See man 1 tput
  • See man 5 terminfofor the complete list of commands and more details on these options. (The corresponding tputcommand is listed in the Cap-namecolumn of the huge table that starts at line 81.)
  • man 1 tput
  • 有关man 5 terminfo这些选项的完整命令列表和更多详细信息,请参阅 参考资料。(对应的tput命令列在Cap-name从第 81 行开始的巨大表的列中。)

回答by Alireza Mirian

A neat way to change color only for one echois to define such function:

一种只为一个改变颜色的巧妙方法echo是定义这样的函数:

function coloredEcho(){
    local exp=;
    local color=;
    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;
}

Usage:

用法:

coloredEcho "This text is green" green

Or you could directly use color codes mentioned in Drew's answer:

或者您可以直接使用Drew 的回答中提到的颜色代码:

coloredEcho "This text is green" 2

回答by Dale Corns

red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"

This answer correct, except that the call to colors should not be inside the quotes.

这个答案是正确的,除了对颜色的调用不应该在引号内。

echo -e ${red}"Hello Stackoverflow"${NC}

Should do the trick.

应该做的伎俩。

回答by Ooker

For readability

为了可读性

If you want to improve the readabilityof the code, you can echothe string first then add the color later by using sed:

如果您想提高代码的可读性,您可以先echo使用字符串,然后使用sed以下方法添加颜色:

echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/' 

回答by Eric Leschinski

These codes work on my Ubuntu box:

这些代码适用于我的 Ubuntu 机器:

enter image description here

在此处输入图片说明

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"

This prints the letters a b c d all in different colors:

这会以不同的颜色打印字母 abcd:

echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"

For loop:

For循环:

for (( i = 0; i < 17; i++ )); 
do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; 
done

enter image description here

在此处输入图片说明

回答by Jorge Bucaran

This is the color switch\033[. See history.

这是颜色开关\033[。见历史

Color codesare like 1;32(Light Green), 0;34(Blue), 1;34(Light Blue), etc.

颜色代码1;32(浅绿色)、0;34(蓝色)、1;34(浅蓝色)等。

We terminate color sequences with a color switch \033[and 0m, the no-color code. Just like opening and closing tabs in a markup language.

我们终止颜色顺序与颜色开关\033[0m中,没有-color代码。就像在标记语言中打开和关闭标签一样。

  SWITCH="3["
  NORMAL="${SWITCH}0m"
  YELLOW="${SWITCH}1;33m"
  echo "${YELLOW}hello, yellow${NORMAL}"

Simple color echofunction solution:

简单的颜色echo函数解决方案:

cecho() {
  local code="3["
  case "" 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=""
  esac
  [ -z "$text" ] && local text="$color${code}0m"
  echo "$text"
}

cecho "Normal"
cecho y "Yellow!"

回答by Shakiba Moshiri

some variables that you can use:

您可以使用的一些变量:

# Reset
Color_Off='3[0m'       # Text Reset

# Regular Colors
Black='3[0;30m'        # Black
Red='3[0;31m'          # Red
Green='3[0;32m'        # Green
Yellow='3[0;33m'       # Yellow
Blue='3[0;34m'         # Blue
Purple='3[0;35m'       # Purple
Cyan='3[0;36m'         # Cyan
White='3[0;37m'        # White

# Bold
BBlack='3[1;30m'       # Black
BRed='3[1;31m'         # Red
BGreen='3[1;32m'       # Green
BYellow='3[1;33m'      # Yellow
BBlue='3[1;34m'        # Blue
BPurple='3[1;35m'      # Purple
BCyan='3[1;36m'        # Cyan
BWhite='3[1;37m'       # White

# Underline
UBlack='3[4;30m'       # Black
URed='3[4;31m'         # Red
UGreen='3[4;32m'       # Green
UYellow='3[4;33m'      # Yellow
UBlue='3[4;34m'        # Blue
UPurple='3[4;35m'      # Purple
UCyan='3[4;36m'        # Cyan
UWhite='3[4;37m'       # White

# Background
On_Black='3[40m'       # Black
On_Red='3[41m'         # Red
On_Green='3[42m'       # Green
On_Yellow='3[43m'      # Yellow
On_Blue='3[44m'        # Blue
On_Purple='3[45m'      # Purple
On_Cyan='3[46m'        # Cyan
On_White='3[47m'       # White

# High Intensity
IBlack='3[0;90m'       # Black
IRed='3[0;91m'         # Red
IGreen='3[0;92m'       # Green
IYellow='3[0;93m'      # Yellow
IBlue='3[0;94m'        # Blue
IPurple='3[0;95m'      # Purple
ICyan='3[0;96m'        # Cyan
IWhite='3[0;97m'       # White

# Bold High Intensity
BIBlack='3[1;90m'      # Black
BIRed='3[1;91m'        # Red
BIGreen='3[1;92m'      # Green
BIYellow='3[1;93m'     # Yellow
BIBlue='3[1;94m'       # Blue
BIPurple='3[1;95m'     # Purple
BICyan='3[1;96m'       # Cyan
BIWhite='3[1;97m'      # White

# High Intensity backgrounds
On_IBlack='3[0;100m'   # Black
On_IRed='3[0;101m'     # Red
On_IGreen='3[0;102m'   # Green
On_IYellow='3[0;103m'  # Yellow
On_IBlue='3[0;104m'    # Blue
On_IPurple='3[0;105m'  # Purple
On_ICyan='3[0;106m'    # Cyan
On_IWhite='3[0;107m'   # White

the escape character in bash, hexand octalrespectively:

bash 中的转义字符,十六进制八进制分别为:

|       | bash  | hex    | octal   | NOTE                         |
|-------+-------+--------+---------+------------------------------|
| start | \e    | \x1b   | 3    |                              |
| start | \E    | \x1B   | -       | x cannot be capital          |
| end   | \e[0m | \x1m0m | 3[0m |                              |
| end   | \e[m  | \x1b[m | 3[m  | 0 is appended if you omit it |
|       |       |        |         |                              |

short example:

简短示例:

| color       | bash         | hex            | octal          | NOTE                                  |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | \e[32m<text> | \x1b[32m<text> | 3[32m<text> | m is NOT optional                     |
| reset       | <text>\e[0m  | <text>xb[0m  | <text>3[om  | o is optional (do it as best practice |
|             |              |                |                |                                       |

bash exception:

bash 异常:

If you are going to use these codes in your special bash variables

如果您打算在您的特殊 bash 变量中使用这些代码

  • PS0
  • PS1
  • PS2 (= this is for prompting)
  • PS4
  • PS0
  • PS1
  • PS2(=这是为了提示)
  • PS4

you should add extra escape characters so that bashcan interpret them correctly. Without this adding extra escape characters it works but you will face problems when you use Ctrl + rfor search in your history.

您应该添加额外的转义字符,以便bash可以正确解释它们。如果不添加额外的转义字符,它就可以工作,但是当您Ctrl + r在历史记录中使用搜索时会遇到问题。

exception rule for bash

bash 的例外规则

You should add \[before any starting ANSI code and add \]after any ending ones.
Example:
in regular usage: \033[32mThis is in green\033[0m
for PS0/1/2/4: \[\033[32m\]This is in green\[\033[m\]

您应该\[在任何起始 ANSI 代码之前添加并\]在任何结束代码之后添加。
示例:
在常规使用中: \033[32mThis is in green\033[0m
对于 PS0/1/2/4: \[\033[32m\]This is in green\[\033[m\]

\[is for start of a sequence of non-printablecharacters
\]is for end of a sequence of non-printablecharacters

\[对于序列的开始不可打印的字符
\]是用于序列的端不可打印字符

Tip: for memorize it you can first add \[\]and then put your ANSI code between them:
- \[start-ANSI-code\]
- \[end-ANSI-code\]

提示:为了记住它,您可以先添加\[\]然后将您的 ANSI 代码放在它们之间:
- \[start-ANSI-code\]
-\[end-ANSI-code\]

type of color sequence:

颜色序列类型:

  1. 3/4 bit
  2. 8 bit
  3. 24 bit
  1. 3/4 位
  2. 8 位
  3. 24 位

Before diving into these colors, you should know about 4 modes with these codes:

在深入研究这些颜色之前,您应该了解这些代码的 4 种模式:

1. color-mode

1. 颜色模式

It modifies the style of color NOT text. For example make the color bright or darker.

它修改了颜色 NOT 文本的样式。例如使颜色变亮或变暗。

  • 0reset
  • 1;lighter than normal
  • 2;darker than normal
  • 0重启
  • 1;比平常轻
  • 2;比正常情况暗

This mode is not supported widely. It is fully support on Gnome-Terminal.

这种模式不被广泛支持。它完全支持 Gnome 终端。

2. text-mode

2. 文本模式

This mode is for modifying the style of text NOT color.

此模式用于修改文本的样式 NOT 颜色。

  • 3;italic
  • 4;underline
  • 5;blinking (slow)
  • 6;blinking (fast)
  • 7;reverse
  • 8;hide
  • 9;cross-out
  • 3;斜体
  • 4;强调
  • 5;闪烁(慢)
  • 6;闪烁(快速)
  • 7;逆转
  • 8;隐藏
  • 9;划掉

and are almost supported.
For example KDE-Konsole supports 5;but Gnome-Terminal does not and Gnome supports 8;but KDE does not.

并且几乎得到支持。
例如 KDE-Konsole 支持5;但 Gnome-Terminal 不支持,Gnome 支持8;但 KDE 不支持。

3. foreground mode

3.前台模式

This mode is for colorizing the foreground.

此模式用于为前景着色。

4. background mode

4.后台模式

This mode is for colorizing the background.

此模式用于为背景着色。

The below table shows a summary of 3/4 bitversion of ANSI-color

下表显示了3/4 位版本的 ANSI 颜色 摘要

|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal    | hex     | bash  | description      | example (= in octal)         | NOTE                                 |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
|          0 | 3[0m  | \x1b[0m | \e[0m | reset any affect | echo -e "3[0m"            | 0m equals to m                       |
|          1 | 3[1m  |         |       | light (= bright) | echo -e "3[1m####3[m"  | -                                    |
|          2 | 3[2m  |         |       | dark (= fade)    | echo -e "3[2m####3[m"  | -                                    |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
|  text-mode | ~        |         |       | ~                | ~                            | ~                                    |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
|          3 | 3[3m  |         |       | italic           | echo -e "3[3m####3[m"  |                                      |
|          4 | 3[4m  |         |       | underline        | echo -e "3[4m####3[m"  |                                      |
|          5 | 3[5m  |         |       | blink (slow)     | echo -e "3[3m####3[m"  |                                      |
|          6 | 3[6m  |         |       | blink (fast)     | ?                            | not wildly support                   |
|          7 | 
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal     | hex       | bash    | description      | example                            | NOTE                    |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
|        0-7 | 3[38;5 | \x1b[38;5 | \e[38;5 | standard. normal | echo -e '3[38;5;1m####3[m'   |                         |
|       8-15 |           |           |         | standard. light  | echo -e '3[38;5;9m####3[m'   |                         |
|     16-231 |           |           |         | more resolution  | echo -e '3[38;5;45m####3[m'  | has no specific pattern |
|    232-255 |           |           |         |                  | echo -e '3[38;5;242m####3[m' | from black to white     |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal     | hex       | bash    | description      | example                            | NOTE                    |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
|        0-7 |           |           |         | standard. normal | echo -e '3[48;5;1m####3[m'   |                         |
|       8-15 |           |           |         | standard. light  | echo -e '3[48;5;9m####3[m'   |                         |
|     16-231 |           |           |         | more resolution  | echo -e '3[48;5;45m####3[m'  |                         |
|    232-255 |           |           |         |                  | echo -e '3[48;5;242m####3[m' | from black to white     |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
3[7m | | | reverse | echo -e "3[7m####3[m" | it affects the background/foreground | | 8 | 3[8m | | | hide | echo -e "3[8m####3[m" | it affects the background/foreground | | 9 | 3[9m | | | cross | echo -e "3[9m####3[m" | | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | foreground | ~ | | | ~ | ~ | ~ | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | 30 | 3[30m | | | black | echo -e "3[30m####3[m" | | | 31 | 3[31m | | | red | echo -e "3[31m####3[m" | | | 32 | 3[32m | | | green | echo -e "3[32m####3[m" | | | 33 | 3[33m | | | yellow | echo -e "3[33m####3[m" | | | 34 | 3[34m | | | blue | echo -e "3[34m####3[m" | | | 35 | 3[35m | | | purple | echo -e "3[35m####3[m" | real name: magenta = reddish-purple | | 36 | 3[36m | | | cyan | echo -e "3[36m####3[m" | | | 37 | 3[37m | | | white | echo -e "3[37m####3[m" | | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | 38 | 8/24 | This is for special use of 8-bit or 24-bit | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | background | ~ | | | ~ | ~ | ~ | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | 40 | 3[40m | | | black | echo -e "3[40m####3[m" | | | 41 | 3[41m | | | red | echo -e "3[41m####3[m" | | | 42 | 3[42m | | | green | echo -e "3[42m####3[m" | | | 43 | 3[43m | | | yellow | echo -e "3[43m####3[m" | | | 44 | 3[44m | | | blue | echo -e "3[44m####3[m" | | | 45 | 3[45m | | | purple | echo -e "3[45m####3[m" | real name: magenta = reddish-purple | | 46 | 3[46m | | | cyan | echo -e "3[46m####3[m" | | | 47 | 3[47m | | | white | echo -e "3[47m####3[m" | | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------| | 48 | 8/24 | This is for special use of 8-bit or 24-bit | | |------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|

The below table shows a summary of 8 bitversion of ANSI-color

下表显示了8 位版本的 ANSI 颜色 摘要

|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal     | hex       | bash    | description | example                                  | NOTE            |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
|      0-255 | 3[38;2 | \x1b[38;2 | \e[38;2 | R = red     | echo -e '3[38;2;255;0;02m####3[m'  | R=255, G=0, B=0 |
|      0-255 | 3[38;2 | \x1b[38;2 | \e[38;2 | G = green   | echo -e '3[38;2;;0;255;02m####3[m' | R=0, G=255, B=0 |
|      0-255 | 3[38;2 | \x1b[38;2 | \e[38;2 | B = blue    | echo -e '3[38;2;0;0;2552m####3[m'  | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal     | hex       | bash    | description | example                                  | NOTE            |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
|      0-255 | 3[48;2 | \x1b[48;2 | \e[48;2 | R = red     | echo -e '3[48;2;255;0;02m####3[m'  | R=255, G=0, B=0 |
|      0-255 | 3[48;2 | \x1b[48;2 | \e[48;2 | G = green   | echo -e '3[48;2;;0;255;02m####3[m' | R=0, G=255, B=0 |
|      0-255 | 3[48;2 | \x1b[48;2 | \e[48;2 | B = blue    | echo -e '3[48;2;0;0;2552m####3[m'  | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|

The 8-bit fast test:
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done

8位快速测试:
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done

The below table shows a summary of 24 bitversion of ANSI-color

下表显示了24 位版本的 ANSI 颜色 摘要

#!/usr/bin/perl -n
print "3[1m3[31m3[36m3[32m3[33m3[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^\w])/g;

some screen-shots

一些屏幕截图

foreground 8-bit summary in a .gif

前景 8 位摘要在一个 .gif

foreground.gif

前景.gif

background 8-bit summary in a .gif

背景 8 位摘要 .gif

background.gif

背景.gif

color summary with their values

颜色摘要及其值

enter image description hereenter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

blinkingon KDE-Terminal

blinking在 KDE 终端上

KDE-blinking

KDE 闪烁

a simple Ccode that shows you more

一个简单的C代码,向您展示更多

cecho_screenshot

cecho_screenshot

a more advanced tool that I developed to deal with these colors:

我开发的一个更高级的工具来处理这些颜色:

bline

线



color-mode shot

色彩模式拍摄

fade-normal-bright

淡入淡出正常明亮

text mode shot

文字模式拍摄

only-text-mode

纯文本模式

combining is OK

组合是可以的

combine

结合

more shots

更多镜头



Tips and Tricks for Advanced Users and Programmers:

高级用户和程序员的提示和技巧:

Can we use these codes in a programming language?

我们可以在编程语言中使用这些代码吗?

Yes, you can. I experienced in bash, c, c++, dperl, python

是的你可以。我在bash, c, c++, d perl, python 方面有经验

Are they slow down the speed of a program?

他们会减慢程序的速度吗?

I think, NO.

我觉得不行。

Can we use these on Windows?

我们可以在 Windows 上使用这些吗?

3/4-bit Yes, if you compile the code with gcc
some screen-shots on Win-7

3/4 位 是的,如果您在 Win-7 上使用一些屏幕截图编译代码gcc

How to calculate the length of code?

如何计算代码长度?

\033[= 2, other parts 1

\033[= 2,其他部分 1

Where can we use these codes?

我们可以在哪里使用这些代码?

Anywhere that has a ttyinterpreter
xterm, gnome-terminal, kde-terminal, mysql-client-CLIand so on.
For example if you want to colorize your output with mysql you can use Perl

在任何地方,有一个tty解释
xtermgnome-terminalkde-terminalmysql-client-CLI等等。
例如,如果您想使用 mysql 为您的输出着色,您可以使用Perl

[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;

store this code in a file name: pcc(= Perl Colorize Character) and then put the file a in valid PATHthen use it anywhere you like.

将此代码存储在文件名中:pcc(= Perl Colorize Character),然后将文件 a 设为有效,PATH然后在您喜欢的任何地方使用它。

ls | pcc
df | pcc

ls | pcc
df | pcc

inside mysqlfirst register it for pagerand then try:

里面mysql首先注册它pager,然后尝试:

echo -e '3[2K'  # clear the screen and do not move the position

pcc

个人电脑

It does NOThandle Unicode.

处理Unicode。

Do these codes only do colorizing?

这些代码只做着色吗?

No, they can do a lot of interesting things. Try:

不,他们可以做很多有趣的事情。尝试:

echo -e '3[2J3[u' # clear the screen and reset the position

or:

或者:

##代码##

There are a lot of beginners that want to clear the screen with system( "clear" )so you can use this instead of system(3)call

有很多初学者想要清除屏幕,system( "clear" )因此您可以使用它而不是system(3)调用

Are they available in Unicode?

它们在 Unicode 中可用吗?

Yes. \u001b

是的。 \u001b

Which version of these colors is preferable?

这些颜色的哪个版本更可取?

It is easy to use 3/4-bit, but it is much accurate and beautiful to use 24-bit.
If you do not have experience with htmlso here is a quick tutorial:
24 bits means: 00000000and 00000000and 00000000. Each 8-bit is for a specific color.
1..8is for and 9..16for and 17..24for
So in html#FF0000means and here it is: 255;0;0
in html#00FF00means which here is: 0;255;0
Does that make sense? what color you want combine it with these three 8-bit values.

它易于使用3/4-bit,但使用起来非常准确和美观24-bit
如果你没有有经验的HTML所以这里是一个快速教程:
24个的装置: 000000000000000000000000。每个 8 位用于特定颜色。
1..89..16用于17..24用于
所以在HTML#FF0000方式,在这里,它是:255;0;0
HTML#00FF00手段在这里是:0;255;0
这是否有意义?你想要什么颜色将它与这三个 8 位值结合起来。



reference:
Wikipedia
ANSI escape sequences
tldp.org
tldp.org
misc.flogisoft.com
some blogs/web-pages that I do not remember

参考:
维基百科
ANSI 转义序列
tldp.org
tldp.org
misc.flogisoft.com
一些我不记得的博客/网页