最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

escape sequence code in Linux Shell Programing

Linux crifan 669浏览 0评论

escape sequence code in Linux Shell Programing

How can I write colorful message on Linux Console? , mostly this kind of question is asked by newcomers (Specially those who are learning shell programming!). As you know in Linux everything is considered as a file, our console is one of such special file. You can write special character sequences to console, which control every aspects of the console like Colors on screen, Bold or Blinking text effects, clearing the screen, showing text boxes etc. For this purpose we have to use special code called escape sequence code. Our Linux console is based on the DEC VT100 serial terminals which support ANSI escape sequence code.

What is special character sequence and how to write it to Console?

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 "33[34m   Hello Colorful World!"
Hello Colorful World!

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

1) First 33, 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.

Note that ANSI escape sequence begins with 33 (Octal value) which is represented as ^[ in termcap and terminfo files of terminals and documentation.

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 "33[escape-code    your-message"

In above syntax you have to use33[ 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 (33) 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). In above example

33Escape character
[Start of CSI
3434 is parameter
mm is letter (specifies action)

Following table show important list of such escape-code/action letter or character

Character or letterUse in CSIExamples
hSet the ANSI modeecho -e "33[h"
lClears the ANSI modeecho -e "33[l"
mUseful to show characters in different colors or effects such as BOLD and Blink, see below for parameter taken by m.echo -e "33[35m Hello World"
qTurns keyboard num lock, caps lock, scroll lock LED on or off, see below.echo -e "33[2q"
sStores the current cursor x,y position (col , row position) and attributesecho -e "33[7s"
uRestores cursor position and attributesecho -e "33[8u"

m understand following parameters

ParameterMeaningExample
0Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc.
1Set BOLD intensity$ echo -e "I am 33[1m BOLD 33[0m Person"
I am BOLD Person
Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (33[0m)
2Set dim intensity$ echo -e "33[1m BOLD 33[2m DIM 33[0m"
5Blink Effect$ echo -e "33[5m Flash! 33[0m"
7Reverse video effect i.e. Black foreground and white background in default color scheme$ echo -e "33[7m Linux OS! Best OS!! 33[0m"
11Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from numeric key pad press 178 and leave both key; nothing will be printed. Now give –> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!)$ press alt + 178
$ echo -e "33[11m"
$ press alt + 178
$ echo -e "33[0m"
$ press alt + 178

25Removes/disables blink effect
27Removes/disables reverse effect
30 – 37Set foreground color
31 – RED
32 – Green
xx – Try to find yourself this left as exercise for you 🙂
$ echo -e "33[31m I am in Red"
40 – 47Set background color
xx – Try to find yourself this left as exercise for you 🙂
$ echo -e "33[44m Wow!!!"

q understand following parameters

ParametersMeaning
0Turns off all LEDs on Keyboard
1Scroll lock LED on and others off
2Num lock LED on and others off
3Caps lock LED on and others off

Click here to see example of q command.

Click here to see example of m command.

Click here to see example of s and u command.

转载请注明:在路上 » escape sequence code in Linux Shell Programing

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
55 queries in 0.219 seconds, using 18.85MB memory