some variables that you can use:
# Reset
Color_Off='33[0m' # Text Reset
# Regular Colors
Black='33[0;30m' # Black
Red='33[0;31m' # Red
Green='33[0;32m' # Green
Yellow='33[0;33m' # Yellow
Blue='33[0;34m' # Blue
Purple='33[0;35m' # Purple
Cyan='33[0;36m' # Cyan
White='33[0;37m' # White
# Bold
BBlack='33[1;30m' # Black
BRed='33[1;31m' # Red
BGreen='33[1;32m' # Green
BYellow='33[1;33m' # Yellow
BBlue='33[1;34m' # Blue
BPurple='33[1;35m' # Purple
BCyan='33[1;36m' # Cyan
BWhite='33[1;37m' # White
# Underline
UBlack='33[4;30m' # Black
URed='33[4;31m' # Red
UGreen='33[4;32m' # Green
UYellow='33[4;33m' # Yellow
UBlue='33[4;34m' # Blue
UPurple='33[4;35m' # Purple
UCyan='33[4;36m' # Cyan
UWhite='33[4;37m' # White
# Background
On_Black='33[40m' # Black
On_Red='33[41m' # Red
On_Green='33[42m' # Green
On_Yellow='33[43m' # Yellow
On_Blue='33[44m' # Blue
On_Purple='33[45m' # Purple
On_Cyan='33[46m' # Cyan
On_White='33[47m' # White
# High Intensity
IBlack='33[0;90m' # Black
IRed='33[0;91m' # Red
IGreen='33[0;92m' # Green
IYellow='33[0;93m' # Yellow
IBlue='33[0;94m' # Blue
IPurple='33[0;95m' # Purple
ICyan='33[0;96m' # Cyan
IWhite='33[0;97m' # White
# Bold High Intensity
BIBlack='33[1;90m' # Black
BIRed='33[1;91m' # Red
BIGreen='33[1;92m' # Green
BIYellow='33[1;93m' # Yellow
BIBlue='33[1;94m' # Blue
BIPurple='33[1;95m' # Purple
BICyan='33[1;96m' # Cyan
BIWhite='33[1;97m' # White
# High Intensity backgrounds
On_IBlack='33[0;100m' # Black
On_IRed='33[0;101m' # Red
On_IGreen='33[0;102m' # Green
On_IYellow='33[0;103m' # Yellow
On_IBlue='33[0;104m' # Blue
On_IPurple='33[0;105m' # Purple
On_ICyan='33[0;106m' # Cyan
On_IWhite='33[0;107m' # White
the escape character in bash, hex and octal respectively:
| | bash | hex | octal | NOTE |
|-------+-------+---------+---------+------------------------------|
| start | e | x1b | 33 | |
| start | E | x1B | - | x cannot be capital |
| end | e[0m | x1b[0m | 33[0m | |
| end | e[m | x1b[m | 33[m | 0 is appended if you omit it |
| | | | | |
short example:
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | e[32m<text> | x1b[32m<text> | 33[32m<text> | m is NOT optional |
| reset | <text>e[0m | <text>1xb[0m | <text>33[om | o is optional (do it as best practice |
| | | | | |
bash exception:
If you are going to use these codes in your special bash variables
- PS0
- PS1
- PS2 (= this is for prompting)
- PS4
you should add extra escape characters so that bash can interpret them correctly. Without this adding extra escape characters it works but you will face problems when you use Ctrl + r
for search in your history.
exception rule for bash
You should add [
before any starting ANSI code and add ]
after any ending ones.
Example:
in regular usage: 33[32mThis is in green33[0m
for PS0/1/2/4: [33[32m]This is in green[33[m]
[
is for start of a sequence of non-printable characters
]
is for end of a sequence of non-printable characters
Tip: for memorize it you can first add []
and then put your ANSI code between them:
[start-ANSI-code]
[end-ANSI-code]
type of color sequence:
- 3/4 bit
- 8 bit
- 24 bit
Before diving into these colors, you should know about 4 modes with these codes:
1. color-mode
It modifies the style of color NOT text. For example make the color bright or darker.
0
reset1;
lighter than normal2;
darker than normal
This mode is not supported widely. It is fully support on Gnome-Terminal.
2. text-mode
This mode is for modifying the style of text NOT color.
3;
italic4;
underline5;
blinking (slow)6;
blinking (fast)7;
reverse8;
hide9;
cross-out
and are almost supported.
For example KDE-Konsole supports 5;
but Gnome-Terminal does not and Gnome supports 8;
but KDE does not.
3. foreground mode
This mode is for colorizing the foreground.
4. background mode
This mode is for colorizing the background.
The below table shows a summary of 3/4 bit version of ANSI-color
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal | hex | bash | description | example (= in octal) | NOTE |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 0 | 33[0m | x1b[0m | e[0m | reset any affect | echo -e "33[0m" | 0m equals to m |
| 1 | 33[1m | | | light (= bright) | echo -e "33[1m####33[m" | - |
| 2 | 33[2m | | | dark (= fade) | echo -e "33[2m####33[m" | - |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| text-mode | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 3 | 33[3m | | | italic | echo -e "33[3m####33[m" | |
| 4 | 33[4m | | | underline | echo -e "33[4m####33[m" | |
| 5 | 33[5m | | | blink (slow) | echo -e "33[3m####33[m" | |
| 6 | 33[6m | | | blink (fast) | ? | not wildly support |
| 7 | 03[7m | | | reverse | echo -e "33[7m####33[m" | it affects the background/foreground |
| 8 | 33[8m | | | hide | echo -e "33[8m####33[m" | it affects the background/foreground |
| 9 | 33[9m | | | cross | echo -e "33[9m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| foreground | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 30 | 33[30m | | | black | echo -e "33[30m####33[m" | |
| 31 | 33[31m | | | red | echo -e "33[31m####33[m" | |
| 32 | 33[32m | | | green | echo -e "33[32m####33[m" | |
| 33 | 33[33m | | | yellow | echo -e "33[33m####33[m" | |
| 34 | 33[34m | | | blue | echo -e "33[34m####33[m" | |
| 35 | 33[35m | | | purple | echo -e "33[35m####33[m" | real name: magenta = reddish-purple |
| 36 | 33[36m | | | cyan | echo -e "33[36m####33[m" | |
| 37 | 33[37m | | | white | echo -e "33[37m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 38 | 8/24 | This is for special use of 8-bit or 24-bit |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| background | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 40 | 33[40m | | | black | echo -e "33[40m####33[m" | |
| 41 | 33[41m | | | red | echo -e "33[41m####33[m" | |
| 42 | 33[42m | | | green | echo -e "33[42m####33[m" | |
| 43 | 33[43m | | | yellow | echo -e "33[43m####33[m" | |
| 44 | 33[44m | | | blue | echo -e "33[44m####33[m" | |
| 45 | 33[45m | | | purple | echo -e "33[45m####33[m" | real name: magenta = reddish-purple |
| 46 | 33[46m | | | cyan | echo -e "33[46m####33[m" | |
| 47 | 33[47m | | | white | echo -e "33[47m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 48 | 8/24 | This is for special use of 8-bit or 24-bit | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
The below table shows a summary of 8 bit version of ANSI-color
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | 33[38;5 | x1b[38;5 | e[38;5 | standard. normal | echo -e '33[38;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[38;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[38;5;45m####33[m' | has no specific pattern |
| 232-255 | | | | | echo -e '33[38;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | | | | standard. normal | echo -e '33[48;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[48;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[48;5;45m####33[m' | |
| 232-255 | | | | | echo -e '33[48;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
The 8-bit fast test:
for code in {0..255}; do echo -e "e[38;05;${code}m $code: Test"; done
The below table shows a summary of 24 bit version of ANSI-color
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | R = red | echo -e '33[38;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | G = green | echo -e '33[38;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | B = blue | echo -e '33[38;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | R = red | echo -e '33[48;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | G = green | echo -e '33[48;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | B = blue | echo -e '33[48;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
some screen-shots
foreground 8-bit summary in a .gif
background 8-bit summary in a .gif
color summary with their values
blinking
on KDE-Terminal
a simple `C` code that shows you more
a more advanced tool that I developed to deal with these colors:
color-mode shot
text mode shot
combining is OK
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++, d perl, python
Do they slow down the speed of a program?
I think, NO.
Can we use these on Windows?
3/4-bit Yes, if you compile the code with gcc
some screen-shots on Win-7
How to calculate the length of code?
33[
= 2, other parts 1
Where can we use these codes?
Anywhere that has a tty
interpreter
xterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
and so on.
For example if you want to colorize your output with mysql you can use Perl
#!/usr/bin/perl -n
print "33[1m33[31m$133[36m$233[32m$333[33m$433[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^w])/g;
store this code in a file name: pcc
(= Perl Colorize Character) and then put the file a in valid PATH
then use it anywhere you like.
ls | pcc
df | pcc
inside mysql
first register it for pager
and then try:
[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;
It does NOT handle Unicode.
Do these codes only do colorizing?
No, they can do a lot of interesting things. Try:
echo -e '33[2K' # clear the screen and do not move the position
or:
echo -e '33[2J33[u' # clear the screen and reset the position
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
Are they available in Unicode?
Yes. 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 html so here is a quick tutorial:
24 bits means: 00000000
and 00000000
and 00000000
. Each 8-bit is for a specific color.
1..8
is for and
9..16
for and
17..24
for
So in html #FF0000
means and here it is:
255;0;0
in html #00FF00
means which here is:
0;255;0
Does that make sense? what color you want combine it with these three 8-bit values.
reference:
Wikipedia
ANSI escape sequences
tldp.org
tldp.org
misc.flogisoft.com
some blogs/web-pages that I do not remember
some variables that you can use:
# Reset
Color_Off='33[0m' # Text Reset
# Regular Colors
Black='33[0;30m' # Black
Red='33[0;31m' # Red
Green='33[0;32m' # Green
Yellow='33[0;33m' # Yellow
Blue='33[0;34m' # Blue
Purple='33[0;35m' # Purple
Cyan='33[0;36m' # Cyan
White='33[0;37m' # White
# Bold
BBlack='33[1;30m' # Black
BRed='33[1;31m' # Red
BGreen='33[1;32m' # Green
BYellow='33[1;33m' # Yellow
BBlue='33[1;34m' # Blue
BPurple='33[1;35m' # Purple
BCyan='33[1;36m' # Cyan
BWhite='33[1;37m' # White
# Underline
UBlack='33[4;30m' # Black
URed='33[4;31m' # Red
UGreen='33[4;32m' # Green
UYellow='33[4;33m' # Yellow
UBlue='33[4;34m' # Blue
UPurple='33[4;35m' # Purple
UCyan='33[4;36m' # Cyan
UWhite='33[4;37m' # White
# Background
On_Black='33[40m' # Black
On_Red='33[41m' # Red
On_Green='33[42m' # Green
On_Yellow='33[43m' # Yellow
On_Blue='33[44m' # Blue
On_Purple='33[45m' # Purple
On_Cyan='33[46m' # Cyan
On_White='33[47m' # White
# High Intensity
IBlack='33[0;90m' # Black
IRed='33[0;91m' # Red
IGreen='33[0;92m' # Green
IYellow='33[0;93m' # Yellow
IBlue='33[0;94m' # Blue
IPurple='33[0;95m' # Purple
ICyan='33[0;96m' # Cyan
IWhite='33[0;97m' # White
# Bold High Intensity
BIBlack='33[1;90m' # Black
BIRed='33[1;91m' # Red
BIGreen='33[1;92m' # Green
BIYellow='33[1;93m' # Yellow
BIBlue='33[1;94m' # Blue
BIPurple='33[1;95m' # Purple
BICyan='33[1;96m' # Cyan
BIWhite='33[1;97m' # White
# High Intensity backgrounds
On_IBlack='33[0;100m' # Black
On_IRed='33[0;101m' # Red
On_IGreen='33[0;102m' # Green
On_IYellow='33[0;103m' # Yellow
On_IBlue='33[0;104m' # Blue
On_IPurple='33[0;105m' # Purple
On_ICyan='33[0;106m' # Cyan
On_IWhite='33[0;107m' # White
the escape character in bash, hex and octal respectively:
| | bash | hex | octal | NOTE |
|-------+-------+---------+---------+------------------------------|
| start | e | x1b | 33 | |
| start | E | x1B | - | x cannot be capital |
| end | e[0m | x1b[0m | 33[0m | |
| end | e[m | x1b[m | 33[m | 0 is appended if you omit it |
| | | | | |
short example:
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | e[32m<text> | x1b[32m<text> | 33[32m<text> | m is NOT optional |
| reset | <text>e[0m | <text>1xb[0m | <text>33[om | o is optional (do it as best practice |
| | | | | |
bash exception:
If you are going to use these codes in your special bash variables
- PS0
- PS1
- PS2 (= this is for prompting)
- PS4
you should add extra escape characters so that bash can interpret them correctly. Without this adding extra escape characters it works but you will face problems when you use Ctrl + r
for search in your history.
exception rule for bash
You should add [
before any starting ANSI code and add ]
after any ending ones.
Example:
in regular usage: 33[32mThis is in green33[0m
for PS0/1/2/4: [33[32m]This is in green[33[m]
[
is for start of a sequence of non-printable characters
]
is for end of a sequence of non-printable characters
Tip: for memorize it you can first add []
and then put your ANSI code between them:
[start-ANSI-code]
[end-ANSI-code]
type of color sequence:
- 3/4 bit
- 8 bit
- 24 bit
Before diving into these colors, you should know about 4 modes with these codes:
1. color-mode
It modifies the style of color NOT text. For example make the color bright or darker.
0
reset1;
lighter than normal2;
darker than normal
This mode is not supported widely. It is fully support on Gnome-Terminal.
2. text-mode
This mode is for modifying the style of text NOT color.
3;
italic4;
underline5;
blinking (slow)6;
blinking (fast)7;
reverse8;
hide9;
cross-out
and are almost supported.
For example KDE-Konsole supports 5;
but Gnome-Terminal does not and Gnome supports 8;
but KDE does not.
3. foreground mode
This mode is for colorizing the foreground.
4. background mode
This mode is for colorizing the background.
The below table shows a summary of 3/4 bit version of ANSI-color
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal | hex | bash | description | example (= in octal) | NOTE |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 0 | 33[0m | x1b[0m | e[0m | reset any affect | echo -e "33[0m" | 0m equals to m |
| 1 | 33[1m | | | light (= bright) | echo -e "33[1m####33[m" | - |
| 2 | 33[2m | | | dark (= fade) | echo -e "33[2m####33[m" | - |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| text-mode | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 3 | 33[3m | | | italic | echo -e "33[3m####33[m" | |
| 4 | 33[4m | | | underline | echo -e "33[4m####33[m" | |
| 5 | 33[5m | | | blink (slow) | echo -e "33[3m####33[m" | |
| 6 | 33[6m | | | blink (fast) | ? | not wildly support |
| 7 | 03[7m | | | reverse | echo -e "33[7m####33[m" | it affects the background/foreground |
| 8 | 33[8m | | | hide | echo -e "33[8m####33[m" | it affects the background/foreground |
| 9 | 33[9m | | | cross | echo -e "33[9m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| foreground | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 30 | 33[30m | | | black | echo -e "33[30m####33[m" | |
| 31 | 33[31m | | | red | echo -e "33[31m####33[m" | |
| 32 | 33[32m | | | green | echo -e "33[32m####33[m" | |
| 33 | 33[33m | | | yellow | echo -e "33[33m####33[m" | |
| 34 | 33[34m | | | blue | echo -e "33[34m####33[m" | |
| 35 | 33[35m | | | purple | echo -e "33[35m####33[m" | real name: magenta = reddish-purple |
| 36 | 33[36m | | | cyan | echo -e "33[36m####33[m" | |
| 37 | 33[37m | | | white | echo -e "33[37m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 38 | 8/24 | This is for special use of 8-bit or 24-bit |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| background | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 40 | 33[40m | | | black | echo -e "33[40m####33[m" | |
| 41 | 33[41m | | | red | echo -e "33[41m####33[m" | |
| 42 | 33[42m | | | green | echo -e "33[42m####33[m" | |
| 43 | 33[43m | | | yellow | echo -e "33[43m####33[m" | |
| 44 | 33[44m | | | blue | echo -e "33[44m####33[m" | |
| 45 | 33[45m | | | purple | echo -e "33[45m####33[m" | real name: magenta = reddish-purple |
| 46 | 33[46m | | | cyan | echo -e "33[46m####33[m" | |
| 47 | 33[47m | | | white | echo -e "33[47m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 48 | 8/24 | This is for special use of 8-bit or 24-bit | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
The below table shows a summary of 8 bit version of ANSI-color
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | 33[38;5 | x1b[38;5 | e[38;5 | standard. normal | echo -e '33[38;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[38;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[38;5;45m####33[m' | has no specific pattern |
| 232-255 | | | | | echo -e '33[38;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | | | | standard. normal | echo -e '33[48;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[48;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[48;5;45m####33[m' | |
| 232-255 | | | | | echo -e '33[48;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
The 8-bit fast test:
for code in {0..255}; do echo -e "e[38;05;${code}m $code: Test"; done
The below table shows a summary of 24 bit version of ANSI-color
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | R = red | echo -e '33[38;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | G = green | echo -e '33[38;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | B = blue | echo -e '33[38;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | R = red | echo -e '33[48;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | G = green | echo -e '33[48;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | B = blue | echo -e '33[48;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
some screen-shots
foreground 8-bit summary in a .gif
background 8-bit summary in a .gif
color summary with their values
blinking
on KDE-Terminal
a simple `C` code that shows you more
a more advanced tool that I developed to deal with these colors:
color-mode shot
text mode shot
combining is OK
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++, d perl, python
Do they slow down the speed of a program?
I think, NO.
Can we use these on Windows?
3/4-bit Yes, if you compile the code with gcc
some screen-shots on Win-7
How to calculate the length of code?
33[
= 2, other parts 1
Where can we use these codes?
Anywhere that has a tty
interpreter
xterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
and so on.
For example if you want to colorize your output with mysql you can use Perl
#!/usr/bin/perl -n
print "33[1m33[31m$133[36m$233[32m$333[33m$433[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^w])/g;
store this code in a file name: pcc
(= Perl Colorize Character) and then put the file a in valid PATH
then use it anywhere you like.
ls | pcc
df | pcc
inside mysql
first register it for pager
and then try:
[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;
It does NOT handle Unicode.
Do these codes only do colorizing?
No, they can do a lot of interesting things. Try:
echo -e '33[2K' # clear the screen and do not move the position
or:
echo -e '33[2J33[u' # clear the screen and reset the position
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
Are they available in Unicode?
Yes. 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 html so here is a quick tutorial:
24 bits means: 00000000
and 00000000
and 00000000
. Each 8-bit is for a specific color.
1..8
is for and
9..16
for and
17..24
for
So in html #FF0000
means and here it is:
255;0;0
in html #00FF00
means which here is:
0;255;0
Does that make sense? what color you want combine it with these three 8-bit values.
reference:
Wikipedia
ANSI escape sequences
tldp.org
tldp.org
misc.flogisoft.com
some blogs/web-pages that I do not remember
some variables that you can use:
# Reset
Color_Off='33[0m' # Text Reset
# Regular Colors
Black='33[0;30m' # Black
Red='33[0;31m' # Red
Green='33[0;32m' # Green
Yellow='33[0;33m' # Yellow
Blue='33[0;34m' # Blue
Purple='33[0;35m' # Purple
Cyan='33[0;36m' # Cyan
White='33[0;37m' # White
# Bold
BBlack='33[1;30m' # Black
BRed='33[1;31m' # Red
BGreen='33[1;32m' # Green
BYellow='33[1;33m' # Yellow
BBlue='33[1;34m' # Blue
BPurple='33[1;35m' # Purple
BCyan='33[1;36m' # Cyan
BWhite='33[1;37m' # White
# Underline
UBlack='33[4;30m' # Black
URed='33[4;31m' # Red
UGreen='33[4;32m' # Green
UYellow='33[4;33m' # Yellow
UBlue='33[4;34m' # Blue
UPurple='33[4;35m' # Purple
UCyan='33[4;36m' # Cyan
UWhite='33[4;37m' # White
# Background
On_Black='33[40m' # Black
On_Red='33[41m' # Red
On_Green='33[42m' # Green
On_Yellow='33[43m' # Yellow
On_Blue='33[44m' # Blue
On_Purple='33[45m' # Purple
On_Cyan='33[46m' # Cyan
On_White='33[47m' # White
# High Intensity
IBlack='33[0;90m' # Black
IRed='33[0;91m' # Red
IGreen='33[0;92m' # Green
IYellow='33[0;93m' # Yellow
IBlue='33[0;94m' # Blue
IPurple='33[0;95m' # Purple
ICyan='33[0;96m' # Cyan
IWhite='33[0;97m' # White
# Bold High Intensity
BIBlack='33[1;90m' # Black
BIRed='33[1;91m' # Red
BIGreen='33[1;92m' # Green
BIYellow='33[1;93m' # Yellow
BIBlue='33[1;94m' # Blue
BIPurple='33[1;95m' # Purple
BICyan='33[1;96m' # Cyan
BIWhite='33[1;97m' # White
# High Intensity backgrounds
On_IBlack='33[0;100m' # Black
On_IRed='33[0;101m' # Red
On_IGreen='33[0;102m' # Green
On_IYellow='33[0;103m' # Yellow
On_IBlue='33[0;104m' # Blue
On_IPurple='33[0;105m' # Purple
On_ICyan='33[0;106m' # Cyan
On_IWhite='33[0;107m' # White
the escape character in bash, hex and octal respectively:
| | bash | hex | octal | NOTE |
|-------+-------+---------+---------+------------------------------|
| start | e | x1b | 33 | |
| start | E | x1B | - | x cannot be capital |
| end | e[0m | x1b[0m | 33[0m | |
| end | e[m | x1b[m | 33[m | 0 is appended if you omit it |
| | | | | |
short example:
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | e[32m<text> | x1b[32m<text> | 33[32m<text> | m is NOT optional |
| reset | <text>e[0m | <text>1xb[0m | <text>33[om | o is optional (do it as best practice |
| | | | | |
bash exception:
If you are going to use these codes in your special bash variables
- PS0
- PS1
- PS2 (= this is for prompting)
- PS4
you should add extra escape characters so that bash can interpret them correctly. Without this adding extra escape characters it works but you will face problems when you use Ctrl + r
for search in your history.
exception rule for bash
You should add [
before any starting ANSI code and add ]
after any ending ones.
Example:
in regular usage: 33[32mThis is in green33[0m
for PS0/1/2/4: [33[32m]This is in green[33[m]
[
is for start of a sequence of non-printable characters
]
is for end of a sequence of non-printable characters
Tip: for memorize it you can first add []
and then put your ANSI code between them:
[start-ANSI-code]
[end-ANSI-code]
type of color sequence:
- 3/4 bit
- 8 bit
- 24 bit
Before diving into these colors, you should know about 4 modes with these codes:
1. color-mode
It modifies the style of color NOT text. For example make the color bright or darker.
0
reset1;
lighter than normal2;
darker than normal
This mode is not supported widely. It is fully support on Gnome-Terminal.
2. text-mode
This mode is for modifying the style of text NOT color.
3;
italic4;
underline5;
blinking (slow)6;
blinking (fast)7;
reverse8;
hide9;
cross-out
and are almost supported.
For example KDE-Konsole supports 5;
but Gnome-Terminal does not and Gnome supports 8;
but KDE does not.
3. foreground mode
This mode is for colorizing the foreground.
4. background mode
This mode is for colorizing the background.
The below table shows a summary of 3/4 bit version of ANSI-color
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal | hex | bash | description | example (= in octal) | NOTE |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 0 | 33[0m | x1b[0m | e[0m | reset any affect | echo -e "33[0m" | 0m equals to m |
| 1 | 33[1m | | | light (= bright) | echo -e "33[1m####33[m" | - |
| 2 | 33[2m | | | dark (= fade) | echo -e "33[2m####33[m" | - |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| text-mode | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 3 | 33[3m | | | italic | echo -e "33[3m####33[m" | |
| 4 | 33[4m | | | underline | echo -e "33[4m####33[m" | |
| 5 | 33[5m | | | blink (slow) | echo -e "33[3m####33[m" | |
| 6 | 33[6m | | | blink (fast) | ? | not wildly support |
| 7 | 03[7m | | | reverse | echo -e "33[7m####33[m" | it affects the background/foreground |
| 8 | 33[8m | | | hide | echo -e "33[8m####33[m" | it affects the background/foreground |
| 9 | 33[9m | | | cross | echo -e "33[9m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| foreground | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 30 | 33[30m | | | black | echo -e "33[30m####33[m" | |
| 31 | 33[31m | | | red | echo -e "33[31m####33[m" | |
| 32 | 33[32m | | | green | echo -e "33[32m####33[m" | |
| 33 | 33[33m | | | yellow | echo -e "33[33m####33[m" | |
| 34 | 33[34m | | | blue | echo -e "33[34m####33[m" | |
| 35 | 33[35m | | | purple | echo -e "33[35m####33[m" | real name: magenta = reddish-purple |
| 36 | 33[36m | | | cyan | echo -e "33[36m####33[m" | |
| 37 | 33[37m | | | white | echo -e "33[37m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 38 | 8/24 | This is for special use of 8-bit or 24-bit |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| background | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 40 | 33[40m | | | black | echo -e "33[40m####33[m" | |
| 41 | 33[41m | | | red | echo -e "33[41m####33[m" | |
| 42 | 33[42m | | | green | echo -e "33[42m####33[m" | |
| 43 | 33[43m | | | yellow | echo -e "33[43m####33[m" | |
| 44 | 33[44m | | | blue | echo -e "33[44m####33[m" | |
| 45 | 33[45m | | | purple | echo -e "33[45m####33[m" | real name: magenta = reddish-purple |
| 46 | 33[46m | | | cyan | echo -e "33[46m####33[m" | |
| 47 | 33[47m | | | white | echo -e "33[47m####33[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 48 | 8/24 | This is for special use of 8-bit or 24-bit | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
The below table shows a summary of 8 bit version of ANSI-color
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | 33[38;5 | x1b[38;5 | e[38;5 | standard. normal | echo -e '33[38;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[38;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[38;5;45m####33[m' | has no specific pattern |
| 232-255 | | | | | echo -e '33[38;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | | | | standard. normal | echo -e '33[48;5;1m####33[m' | |
| 8-15 | | | | standard. light | echo -e '33[48;5;9m####33[m' | |
| 16-231 | | | | more resolution | echo -e '33[48;5;45m####33[m' | |
| 232-255 | | | | | echo -e '33[48;5;242m####33[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
The 8-bit fast test:
for code in {0..255}; do echo -e "e[38;05;${code}m $code: Test"; done
The below table shows a summary of 24 bit version of ANSI-color
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | R = red | echo -e '33[38;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | G = green | echo -e '33[38;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[38;2 | x1b[38;2 | e[38;2 | B = blue | echo -e '33[38;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | R = red | echo -e '33[48;2;255;0;02m####33[m' | R=255, G=0, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | G = green | echo -e '33[48;2;;0;255;02m####33[m' | R=0, G=255, B=0 |
| 0-255 | 33[48;2 | x1b[48;2 | e[48;2 | B = blue | echo -e '33[48;2;0;0;2552m####33[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
some screen-shots
foreground 8-bit summary in a .gif
background 8-bit summary in a .gif
color summary with their values
blinking
on KDE-Terminal
a simple `C` code that shows you more
a more advanced tool that I developed to deal with these colors:
color-mode shot
text mode shot
combining is OK
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++, d perl, python
Do they slow down the speed of a program?
I think, NO.
Can we use these on Windows?
3/4-bit Yes, if you compile the code with gcc
some screen-shots on Win-7
How to calculate the length of code?
33[
= 2, other parts 1
Where can we use these codes?
Anywhere that has a tty
interpreter
xterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
and so on.
For example if you want to colorize your output with mysql you can use Perl
#!/usr/bin/perl -n
print "33[1m33[31m$133[36m$233[32m$333[33m$433[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^w])/g;
store this code in a file name: pcc
(= Perl Colorize Character) and then put the file a in valid PATH
then use it anywhere you like.
ls | pcc
df | pcc
inside mysql
first register it for pager
and then try:
[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;
It does NOT handle Unicode.
Do these codes only do colorizing?
No, they can do a lot of interesting things. Try:
echo -e '33[2K' # clear the screen and do not move the position
or:
echo -e '33[2J33[u' # clear the screen and reset the position
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
Are they available in Unicode?
Yes. 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 html so here is a quick tutorial:
24 bits means: 00000000
and 00000000
and 00000000
. Each 8-bit is for a specific color.
1..8
is for and
9..16
for and
17..24
for
So in html #FF0000
means and here it is:
255;0;0
in html #00FF00
means which here is:
0;255;0
Does that make sense? what color you want combine it with these three 8-bit values.
reference:
Wikipedia
ANSI escape sequences
tldp.org
tldp.org
misc.flogisoft.com
some blogs/web-pages that I do not remember
В UNIX-подобных операционных системах команда echo используется для вывода строки символов на терминал.
Обычно цвет вывода команды echo повторяет тему терминала, в этом нет ничего необычного.
Но если вы хотите, вы можете изменить цвет выводимого текста команды echo в Linux.
Почему вы хотите это сделать?
Для этого может быть несколько причин.
Например, если вы пишете скрипт, в котором есть шаг, где нужно предупредить пользователя, использование вывода красного цвета может быть полезным.
Сейчас я расскажу вам, как можно изменить цвет текста с черного или белого на другой, например, желтый, красный, зеленый.
Эскейп-коды ANSI для цветного вывода
Чтобы изменить цвет вывода команды echo, необходимо использовать управляющие последовательности.
Популярный пример управляющей последовательности, которую вы, возможно, использовали в прошлом, это то, что мы используем для новой строки, ‘n’.
Аналогично, существуют управляющие последовательности и для задания цветного вывода.
Ниже приведено несколько кодов 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
Вы можете использовать эти коды с помощью ‘33[‘, за которым следует ANSI-код выбранного вами цвета и, наконец, ‘m’.
Ниже приведено несколько примеров того, как использовать код цвета ANSI:
R='33[0;31m' #'0;31' is Red's ANSI color code
G='33[0;32m' #'0;32' is Green's ANSI color code
Y='33[1;32m' #'1;32' is Yellow's ANSI color code
B='33[0;34m' #'0;34' is Blue's ANSI color code
Использование этого символа похоже на то, как вы используете обратный слеш ‘’ с ‘n’ для обозначения новой строки.
Цветовое оформление вывода в команде echo
Вы можете использовать указанные цвета в качестве “переменной” в команде echo и указывать их перед любым текстом, чтобы текст отображался с указанным цветом в выводе терминала.
Если вы используете специальные управляющие последовательности, такие как ‘n’, ‘t’, или эти управляющие последовательности для цветов в команде echo, пожалуйста, убедитесь, что вы используете флаг “-e” в команде echo.
В этом примере я объявил две переменные с кодами цветов.
Первая – для красного цвета, а ‘NOCOLOR’ – для того, чтобы остановить красный цвет и переключиться обратно на цвет по умолчанию.
Как вы можете видеть, он отображал текст ‘love’ красным цветом.
Я использовал его непосредственно в оболочке.
Вы можете поместить его в сценарий таким же образом:
#!/bin/bash
RED='33[0;31m'
NOCOLOR='33[0m'
echo -e "I ${RED}love${NOCOLOR} Linux"
Вы также можете добавить цвет к выводу printf.
Если вам нужна отформатированная строка, лучше использовать команду printf вместо команды echo.
После того как вы определите управляющие последовательности цвета – точно такие же, как для echo, – вы можете использовать их и в printf.
Здесь я сделал то же самое, что и в случае с echo, я указал и использовал переменную ‘NOCOLOR’, чтобы прекратить использование красного цвета и переключиться обратно на цвет по умолчанию после слова “love”.
Бонусный совет: Используйте онлайн-инструмент для генерации цветов
Пользователь GitHub Gbox4 создал чрезвычайно полезный веб-сайт под названием “ANSI escape code generator“, который помогает генерировать эти управляющие последовательности для вас.
Он позволяет выбрать один из множества цветов для фона и переднего плана текста (цвет текста), стили – жирный, курсив, подчеркивание или зачеркивание.
Затем вы можете легко сохранить этот автоматически сгенерированный код в переменную bash для дальнейшего использования.
Заключение
Теперь, когда вы знаете, как добавить цвета в вывод команды echo, вы можете сделать свои cкрипты bash более полезными и привлекательными.
Поскольку команда printf более предсказуема, позволяет форматировать наш вывод и очень настраиваема, если у вас есть конкретная задача, я бы рекомендовал использовать printf.
Если же вы решаете простую задачу, используйте команду echo.
см. также:
- 📜 Добавление цветов в скрипты Bash
- 🏌 Команда Echo в Linux с примерами
- 🐧 Команда Bash Printf
- 🐧 Команда wait в Linux с примерами
- 🐧 Команда Xargs в Linux
I know that for printing a colored text using echo, for example red color, the code is: echo -e "e[1;31m This is red text e[0m"
and I know that in this example, 31 is code of red color and the number of other colors is:
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37
and for background colors, the num is 40 to 47
my Question is this: what does e
and [
and m
(after 31) exactly mean here?
I read in the man page that e is escape, but I didn’t understand what it means.
asked Jul 18, 2012 at 18:37
2
To be precise color codes are defined as below-
Opening
RED= "33[31m" or "e[1;31m"
CYAN="33[36m" or "e[1;31m"
Closing = "33[m" or "e[0m"
So basically its just the way different terminal interpret color codes and you caption your text within these elements. Its an odd way to make the terminal understand what you mean (like HexCodes or Binary..)
answered Jul 18, 2012 at 21:38
Anjan BiswasAnjan Biswas
2061 gold badge3 silver badges10 bronze badges
I found a nice explanation here (at least, nice for me ). I’m not sure if it’ll be useful for you.
To start, type this on your console:
echo "^[[0;31;40mIn Color"
The first character is an escape character, which looks like two characters, ^ and [. To be able to print this, you have to press CTRL+V and then the ESC key. All the other characters are normal printable characters, so you see the string In Color in red. The type stays that color until you revery back by typing this:
They are escape codes to set the color modes in the terminal. Is just that when printed with regular characters, they look like that.
bahamat
37.5k4 gold badges70 silver badges103 bronze badges
answered Jul 18, 2012 at 21:28
BorrajaXBorrajaX
1,2111 gold badge17 silver badges24 bronze badges
e
is shorthand for the Escape character. the <ESC>[
and the m
are the markers for the beginning and end of ANSI color control sequences, which are a subset of ANSI terminal control sequenced. Most of them start with <ESC>[
, followed by a number, followed by a letter indicating which function is being implemented (e. g. move cursor to line; clear to end of line, return cursor to saved location, et cetera).
answered Jul 30, 2012 at 19:26
DopeGhotiDopeGhoti
71.4k8 gold badges94 silver badges132 bronze badges