ANSI escape sequences

 

ANSI escape sequences are characters embedded in the text used to control formatting, color, and other output options on video text terminals. Almost all terminal emulators designed to show text output from a remote computer, and (except for Windows) to show text output from local software, interpret at least some of the ANSI escape sequences.

      By default what ever you send to console it is printed as its. For e.g. consider following echo statement,
$ echo "Hello World"
Hello World
      Above echo statement prints sequence of character on screen, but if there is any special escape sequence (control character) in sequence , then first some action is taken according to escape sequence (or control character) and then normal character is printed on console. For e.g. following echo command prints message in Blue color on console
$ echo -e "\033[34m   Hello Colorful  World!"
Hello Colorful  World!


      Above echo statement uses ANSI escape sequence (\033[34m), above entire string ( i.e.  "\033[34m   Hello Colorful  World!" ) is process as follows

1) First \033, is escape character, which causes to take some action
2) Here it set screen foreground color to Blue using [34m escape code.
3) Then it prints our normal message Hello Colorful  World! in blue color.

 

      Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033). You can use echo statement to print message, to use ANSI escape sequence you must use -e option (switch) with echo statement, general syntax is as follows Syntax:
echo   -e  "\033[escape-code    your-message"  

 

      In above syntax you have to use\033[ as its with different escape-code for different operations. As soon as console receives the message it start to process/read it, and if it found escape character (\033) it moves to escape mode, then it read "[" character and moves into Command Sequence Introduction (CSI) mode. In CSI mode console reads a series of ASCII-coded decimal numbers (know as parameter) which are separated by semicolon (;) . This numbers are read until console action letter or character is not found (which determines what action to take).

 

Character or letter

Use in CSI

Examples

h

Set the ANSI mode

echo -e "\033[h"

l

Clears the ANSI mode

echo -e "\033[l"

m

Useful to show characters in different colors or effects such as BOLD and Blink, see below for parameter taken by m.

echo -e  "\033[35m Hello World"

q

Turns keyboard num lock, caps lock, scroll lock LED on or off, see below.

echo -e "\033[2q"

s

Stores the current cursor x,y position (col , row position) and attributes

echo -e "\033[7s"

u

Restores cursor position and attributes

echo -e "\033[8u"

 

And m understood following parameters:

 

reset all attributes to their defaults 

set bold 

set half-bright (simulated with color on a color display) 

set underscore (simulated with color on a color display)(the colors used to simulate dim or underline are set using ESC ] ...) 

set blink 

set reverse video 

10 

reset selected mapping, display control flag, and toggle meta flag (ECMA-48 says "primary font"). 

11 

select null mapping, set display control flag, reset toggle meta flag (ECMA-48 says "first alternate font"). 

12 

select null mapping, set display control flag, set toggle meta flag (ECMA-48 says "second alternate font"). The toggle   meta flag causes the high bit of a byte to be toggled before the mapping table translation is done. 

21 

 set normal intensity (ECMA-48 says "doubly underlined") 

22 

set normal intensity 

24 

underline off 

25 

blink off 

27 

reverse video off 

30 

set black foreground 

31 

set red foreground 

32 

set green foreground 

33 

set brown foreground 

34 

set blue foreground 

35 

set magenta foreground 

36 

set cyan foreground 

37 

set white foreground 

38 

set underscore on, set default foreground color 

39 

set underscore off, set default foreground color 

40 

set black background 

41 

set red background 

42 

set green background 

43 

set brown background 

44 

set blue background 

45 

set magenta background 

46    

set cyan background 

47 

set white background 

49 

set default background color

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值