# 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
for bash
add \[
before any starting ANSI code and add\]
ansi SGR (Select Graphic Rendition) subset
\033[XXXm
where XXX
is a series of semicolon-separated parameters.
echo -e "\033[31;1;4mHello\033[0m"
where the first part makes the text red (31
), bold (1
), underlined (4
) and the last part clears all this (0
).
Code | Effect | Note |
---|---|---|
0 | Reset / Normal | all attributes off |
1 | Bold or increased intensity | |
2 | Faint (decreased intensity) | Not widely supported. |
3 | Italic | Not widely supported. Sometimes treated as inverse. |
4 | Underline | |
5 | Slow Blink | less than 150 per minute |
6 | Rapid Blink | MS-DOS ANSI.SYS; 150+ per minute; not widely supported |
7 | [[reverse video]] | swap foreground and background colors |
8 | Conceal | Not widely supported. |
9 | Crossed-out | Characters legible, but marked for deletion. Not widely supported. |
10 | Primary(default) font | |
11–19 | Alternate font | Select alternate font n-10 |
20 | Fraktur | hardly ever supported |
21 | Bold off or Double Underline | Bold off not widely supported; double underline hardly ever supported. |
22 | Normal color or intensity | Neither bold nor faint |
23 | Not italic, not Fraktur | |
24 | Underline off | Not singly or doubly underlined |
25 | Blink off | |
27 | Inverse off | |
28 | Reveal | conceal off |
29 | Not crossed out | |
30–37 | Set foreground color | See color table below |
38 | Set foreground color | Next arguments are 5;<n> or 2;<r>;<g>;<b> , see below |
39 | Default foreground color | implementation defined (according to standard) |
40–47 | Set background color | See color table below |
48 | Set background color | Next arguments are 5;<n> or 2;<r>;<g>;<b> , see below |
49 | Default background color | implementation defined (according to standard) |
51 | Framed | |
52 | Encircled | |
53 | Overlined | |
54 | Not framed or encircled | |
55 | Not overlined | |
60 | ideogram underline | hardly ever supported |
61 | ideogram double underline | hardly ever supported |
62 | ideogram overline | hardly ever supported |
63 | ideogram double overline | hardly ever supported |
64 | ideogram stress marking | hardly ever supported |
65 | ideogram attributes off | reset the effects of all of 60-64 |
90–97 | Set bright foreground color | aixterm (not in standard) |
100–107 | Set bright background color | aixterm (not in standard) |
colors
3-bit and 4-bit
The original specification only had 8 colors, and just gave them names.
The SGR parameters 30–37 selected the foreground color,
40–47 selected the background.
Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font,
thus providing 8 additional foreground colors.
Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that.
Examples:
to get black letters on white background use ESC[30;47m
,
to get red useESC[31m
, to get bright red useESC[1;31m
.
To reset colors to their defaults, use ESC[39;49m
(not supported on some terminals),
or reset all attributes with ESC[0m
.
Later terminals added the ability to directly specify the "bright" colors with 90–97 and 100–107.
When hardware started using 8-bit digital-to-analog converters (DACs) several pieces of software assigned 24-bit color numbers to these names. The chart below shows the default values sent to the DAC for some common hardware and software; in most cases they are configurable
8-bit
As 256-color lookup tables became common on graphic cards,
escape sequences were added to select from a pre-defined set of 256 colors:
ESC[38;5;⟨n⟩m Select foreground color where n is a number from the table below ESC[48;5;⟨n⟩m Select background color 0- 7: standard colors (as in ESC [ 30–37 m) 8- 15: high intensity colors (as in ESC [ 90–97 m) 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) 232-255: grayscale from dark to light in 24 steps
The ITU's T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures uses ":" as separator characters instead:
ESC[38:5:⟨n⟩m Select foreground color where n is a number from the table below ESC[48:5:⟨n⟩m Select background color
There has also been a similar but incompatible 88-color encoding using the same escape sequence, seen in rxvt
and xterm-88color
. Not much is known about the scheme besides the color codes. It uses a 4×4×4 color cube.
24-bit
As "true color" graphic cards with 16 to 24 bits of color became common, applications began to support 24-bit colors.
Terminal emulators supporting setting 24-bit foreground and background colors with escape sequences include Xterm,Konsole and iTerm,any libvte based terminals eg GNOME Terminal
ESC[38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color ESC[48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
ESC[38:2:⟨Color-Space-ID⟩:⟨r⟩:⟨g⟩:⟨b⟩:⟨unused⟩:⟨CS tolerance⟩:⟨Color-Space associated with tolerance: 0 for "CIELUV"; 1 for "CIELAB"⟩ m Select RGB foreground color ESC[48:2:⟨Color-Space-ID⟩:⟨r⟩:⟨g⟩:⟨b⟩:⟨unused⟩:⟨CS tolerance⟩:⟨Color-Space associated with tolerance: 0 for "CIELUV"; 1 for "CIELAB"⟩ m Select RGB background color
The parameters after the '2' (r, g, and b) are optional and can be left empty.
OSC (Operating System Command) sequences
Most Operating System Command sequences were defined by Xterm, but
many are also supported by other terminal emulators. For historical
reasons, Xterm can end the command with BEL
as well as the standard ST
. For example, Xterm allows the window title to be set by ESC ]0;this is the window title BEL
.
hyperlink, ESC ]8;;link ST
Linux console uses ESC ] P n rr gg bb
to change the palette
flasher () { while true; do printf \\e[?5h; sleep 0.1; printf \\e[?5l; read -s -n1 -t1 && break; done; }
reset console
printf \\033c
Terminal input sequences
<char> -> char <esc> <nochar> -> esc <esc> <esc> -> esc <esc> <char> -> Alt-keypress or keycode sequence <esc> '[' <nochar> -> Alt-[ <esc> '[' (<modifier>) <char> -> keycode sequence, <modifier> is a decimal number and defaults to 1 (xterm) <esc> '[' (<keycode>) (';'<modifier>) '~' -> keycode sequence, <keycode> and <modifier> are decimal numbers and default to 1 (vt)
or example,
<esc>[4;2~
is Shift+End,
<esc>[20~
is function key F9,
<esc>[5C
is Ctrl+→.
, the modifier is the sum of the following numbers:
Key pressed | Number | Comment |
---|---|---|
|
1 | always added, the rest are optional |
Shift | 1 |
|
(Left) Alt | 2 |
|
Control | 4 |
|
Meta | 8 |
|
vt sequences: <esc>[1~ - Home <esc>[16~ - <esc>[31~ - F17 <esc>[2~ - Insert <esc>[17~ - F6 <esc>[32~ - F18 <esc>[3~ - Delete <esc>[18~ - F7 <esc>[33~ - F19 <esc>[4~ - End <esc>[19~ - F8 <esc>[34~ - F20 <esc>[5~ - PgUp <esc>[20~ - F9 <esc>[35~ - <esc>[6~ - PgDn <esc>[21~ - F10 <esc>[7~ - Home <esc>[22~ - <esc>[8~ - End <esc>[23~ - F11 <esc>[9~ - <esc>[24~ - F12 <esc>[10~ - F0 <esc>[25~ - F13 <esc>[11~ - F1 <esc>[26~ - F14 <esc>[12~ - F2 <esc>[27~ - <esc>[13~ - F3 <esc>[28~ - F15 <esc>[14~ - F4 <esc>[29~ - F16 <esc>[15~ - F5 <esc>[30~ - xterm sequences: <esc>[A - Up <esc>[K - <esc>[U - <esc>[B - Down <esc>[L - <esc>[V - <esc>[C - Right <esc>[M - <esc>[W - <esc>[D - Left <esc>[N - <esc>[X - <esc>[E - <esc>[O - <esc>[Y - <esc>[F - End <esc>[1P - F1 <esc>[Z - <esc>[G - Keypad 5 <esc>[1Q - F2 <esc>[H - Home <esc>[1R - F3 <esc>[I - <esc>[1S - F4 <esc>[J - <esc>[T -
export PS1='\n\[\033[0;32m\]\u@\h \[\033[1;33m\]\w\n\[\033[0m\]\$'
newline=\n
Green='\033[0;32m' # Green
BYellow='\033[1;33m' # bold Yellow
Color_Off='\033[0m' # Text Reset
\u = the username of the current user
\h = the hostname up to the first `.'
\w = the value of the PWD shell variable ($PWD), with
$HOME abbreviated with a tilde (uses the value of
the PROMPT_DIRTRIM variable)
\$ = # if uid=0 else $
https://en.wikipedia.org/wiki/ANSI_escape_code
https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
https://wiki.archlinux.org/title/Bash/Prompt_customization
https://man.archlinux.org/man/bash.1#PROMPTING
Comments
Post a Comment