Snabbreferens

ANSI escape-koder Snabbguide: Terminalfärger, formatering och markör

En snabbguide för ANSI escape-sekvenser i terminalen: textformatering, färger, markörpositionering och skärmrensning.

· Publicerad

Escape Sequence Syntax

In shell scripts, the escape character is typically represented as \033, \e, or \x1b:

\033[<parameters>m

Text Styles

Code Effect
0 Reset / Normal (clears all styles)
1 Bold / Increased intensity
2 Dim / Decreased intensity
3 Italic (not widely supported)
4 Underline
5 Slow Blink
7 Reverse Video (invert foreground & background)
8 Conceal / Hidden
9 Crossed-out / Strikethrough

Standard Colors (3-bit / 4-bit)

Color Name Foreground (Normal) Foreground (Bright) Background (Normal) Background (Bright)
Black 30 90 40 100
Red 31 91 41 101
Green 32 92 42 102
Yellow 33 93 43 103
Blue 34 94 44 104
Magenta 35 95 45 105
Cyan 36 96 46 106
White 37 97 47 107

Examples

# Red bold text
echo -e "\033[1;31mError:\033[0m Something went wrong."

# Green text on black background
echo -e "\033[32;40mSuccess!\033[0m"

Cursor Movement & Screen Clearing

Sequence Action
\033[<n>A Move cursor up <n> lines
\033[<n>B Move cursor down <n> lines
\033[<n>C Move cursor forward (right) <n> columns
\033[<n>D Move cursor backward (left) <n> columns
\033[<line>;<col>H Move cursor to row <line>, column <col>
\033[2J Clear entire screen and home cursor
\033[K or \033[0K Clear line from cursor position to end
\033[1K Clear line from cursor position to beginning
\033[2K Clear entire current line

Helpful Bash Utilities

# Cursor visibility
hide_cursor() { printf "\e[?25l"; }
show_cursor() { printf "\e[?25h"; }

# Text formatting helpers
bold()  { printf "\e[1m%s\e[0m\n" "$*"; }
green() { printf "\e[32m%s\e[0m\n" "$*"; }
red()   { printf "\e[31m%s\e[0m\n" "$*"; }

Relaterade ämnen