Konubinix' opinionated web of thoughts

Ansi (Not Ascii) Escape Code Terminal Move Cursor and Change Colors in Bash

Fleeting

Linux tips: set terminal character display color and move cursor position in code - Develop Paper

ANSI escape codes can be used to set character display color, move cursor position, clear character display and so on.

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

basic format is as follows: Esc[escape code

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

used\eEscape character to represent the ESC character

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

Bash shell, you can useechoImperative-eOption to test the ANSI escape code

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

ASCII encoding, the octal value of escape character is 033.

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

Then inechoIn the order,\033Represents the escape character.

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

printf command can also output ANSI escape code without adding-eOptions, such as ` printf “E [31m].

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

also be written asecho -e “\e[31m”,

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

implementecho -e “\e[0m"Command to reset the terminal properties

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

basic format of ANSI escape code for setting terminal character color is as follows: Esc[Value;…;Valuem

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

value colour

30 black

31 gules

32 green

33 yellow

34 blue

35 violet

36 Cyan

37 white

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

value colour

40 black

41 gules

42 green

43 yellow

44 blue

45 violet

46 Cyan

47 white

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

value Attribute meaning

0 Reset all properties, including character colors

1 Make it bold

4 Underline

5 Flash on

7 Color reversal

8 Show invisible text

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

The given foreground color and background color do not require sequence.

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

Escape code meaning

Esc[nA Move the cursor up n rows, and the number of columns remains unchanged. Move to the top of the terminal and do not move again

Esc[nB Move the cursor down n rows, and the number of columns remains unchanged. Move to the bottom of the terminal and do not move again

Esc[nC The cursor moves n columns to the right, and the number of rows remains unchanged. Move to the far right of the terminal and do not move again

Esc[nD The cursor moves n columns to the left, and the number of rows remains unchanged. Move to the left end of the terminal and do not move again

Esc[nE Move the cursor down n rows, and the number of columns changes to the beginning of the row

Esc[nF Move the cursor up n rows, and the number of columns changes to the beginning of the row

Esc[Line;ColumnH Move the cursor to the specified number of rows and columns. If no value is provided, the default value is 0

Esc[ColumnG Move the cursor to the column with the current number of rows unchanged

Esc[s Save the current cursor position, and then use ESC [u] to jump to the saved position

Esc[u Jump to the cursor position saved by ESC [S]

Esc[?25l Hide cursor (lowercase L after 25)

Esc[?25h Show cursor

https://developpaper.com/linux-tips-set-terminal-character-display-color-and-move-cursor-position-in-code/

tput

To ease doing all that stuff.

CYAN="$(tput setaf 6)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
OFF="$(tput sgr0)"
BOL="$(tput cr)"
KILL_LINE="$(tput el)"
echo -e "${RED}This is something that will be cleared ${BOL}${KILL_LINE}${CYAN}This is some ${OFF}other thing"

change the prompt color

You can also change color of the PS1 prompt using tput as shown below:

$ export PS1=”\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]“

tput Color Capabilities: tput setab [1-7] – Set a background color using ANSI escape tput setb [1-7] – Set a background color tput setaf [1-7] – Set a foreground color using ANSI escape tput setf [1-7] – Set a foreground color tput Text Mode Capabilities: tput bold – Set bold mode tput dim – turn on half-bright mode tput smul – begin underline mode tput rmul – exit underline mode tput rev – Turn on reverse mode tput smso – Enter standout mode (bold on rxvt) tput rmso – Exit standout mode tput sgr0 – Turn off all attributes

Color Code for tput: 0 – Black 1 – Red 2 – Green 3 – Yellow 4 – Blue 5 – Magenta 6 – Cyan 7 – White

https://linux.101hacks.com/ps1-examples/prompt-color-using-tput/