Linux彩色Banner



Linux彩色Banner

比如banner改为红色的:

echo -e “\e[1;31m Colorful text”

恢复成默认的
echo -e “\e[0m Reset”

颜色代码:

Learn how to make the text in your xterm blink magenta against a cyan background–even in your C programs.
Have you ever redirected the output of a curses program with colors and wondered what those mysterious ^[[ symbols are? Have you ever tried to produce colors with a printf command without using curses? If the answer to either of these questions is yes, read on. This article attempts to explain the mysterious characters that one finds in the output of a curses program that produces colors. Later, we extend this concept to produce colors with a mere printf command.

Terminal Codes
In the old days of teletype terminals, terminals were located far away from computers and were connected to them through serial cables. The terminals could be configured by sending a series of bytes to each one. All of the capabilities of terminals could be accessed through these series of bytes, which usually are called escape sequences because they start with an escape (0x1B) character. Even today, with vt100 emulation, we can send escape sequences to the emulator that have the same effect on the terminal window. Hence, in order to print color, we merely echo a control code.

To start, type this on your console:

echo “^[[0;31;40mIn Color”
(本人注:^[为ESC按键,我不知道怎么在终端上输出ESC按键,但是可以使用\e替代,如上命令可以表示为如下:
echo -e ‘\e[0;31;40mIn color’
不过我一般写成echo -e ‘\e[0;31;40mIn color\e[0m’,后面的\e[0m表示恢复成默认颜色)
The first character is an escape character, which looks like two characters, ^ and [. To be able to print this, you have to press CTRL+V and then the ESC key. All the other characters are normal printable characters, so you see the string In Color in red. The type stays that color until you revery back by typing this:

echo “^[[0;37;40m”

As you can see, it is easy to set and reset colors in a console or xterm. A myriad of escape sequences are available with which you can do a lot of things, including moving the cursor and resetting the terminal.

The Color Code: <ESC>[{attr};{fg};{bg}m
Now, I explain the escape sequence used to produce colors. The sequence to be printed or echoed to the terminal is

<ESC>[{attr};{fg};{bg}m

The first character is ESC, which has to be entered by pressing CTRL+V and then ESC on the Linux console or in xterm, konsole, kvt and so on. Incidentally, CTRL+V ESC also is the combination used to embed an Esc character in a document in Vim. Then, {attr}, {fg} and {bg} have to be replaced with the correct value to achieve the corresponding effect. attr is the attribute, such as blinking or underlined text, while fg and bg are foreground and background colors, respectively. You don’t have to put braces around the number; simply writing the number is sufficient.

{attr} needs to be one of the following:

0 Reset All Attributes (return to normal mode)

1 Bright (usually turns on BOLD)

2 Dim

3 Underline

5 Blink

7 Reverse

8 Hidden

{fg} needs to be one of the following:


30 Black

31 Red

32 Green

33 Yellow

34 Blue

35 Magenta

36 Cyan

37 White

{bg} needs to be one of the following:

40 Black

41 Red

42 Green

43 Yellow

44 Blue

45 Magenta

46 Cyan

47 White

So, to get a blinking line with a blue foreground and a green background, the combination should be:
echo “^[[5;34;42mIn color”

which actually is very ugly. So, revert back with

echo “^[0;37;40m”

一般,我们会利用上述方法来更改linxu的登录banner,比如/etc/issue,下面是一个修改实例,请直接在终端中执行,然后使用cat issue看看效果:
echo -e ‘\e[H\e[2J’ > issue
echo -e ‘ \e[1;30m| \e[34m\\s \\r’ >> issue
echo -e ‘ \e[36;1m/\\\\ \e[37m|| \e[36m| | \e[30m|’ >> issue
echo -e ‘ \e[36m/ \\\\ \e[37m|| \e[36m| _ \e[30m| \e[32m\\t’ >> issue
echo -e ‘ \e[1;36m/ \e[0;36m.. \e[1m\\\\ \e[37m//==\\\\\\\\ ||/= /==\\\\ ||/=\\\\ \e[36m| | |/ \\\\ | | \\\\ / \e[30m| \e[32m\\d’ >> issue
echo -e ‘ \e[0;36m/ . . \\\\ \e[37m|| || || | || || \e[36m| | | | | | X \e[1;30m|’ >> issue
echo -e ‘ \e[0;36m/ . . \\\\ \e[37m\\\\\\\\==/| || \\\\==/ || || \e[36m| | | | \\\\_/| / \\\\ \e[1;30m| \e[31m\\U’ >> issue
echo -e ‘ \e[0;36m/ .. .. \\\\ \e[0;37mA simple, lightweight linux distribution. \e[1;30m|’ >> issue
echo -e ‘ \e[0;36m/_\x27 `_\\\\ \e[1;30m| \e[35m\\l \e[0mon \e[1;33m\\n’ >> issue
echo -e ‘ \e[0m’ >> issue
echo -e ” >> issue