Shell script output with color content __shell

Source: Internet
Author: User

This article mainly describes how to output colored content in a shell script.

In a shell script, both Echo and printf can output content. Let's look at the following example

Echo-e "\033[43;35m David use echo say hello world \033[0m \ n" 

printf "\033[44;36m David use printf say Hello world \ 033[0m \ n " 

echo-e" \033[47;30;5m David use echo say \033[0m Hello world \ n "

The output is as follows:

Its format is this:

ECHO-E "\033[background color, font color m string \033[0m"  or
printf "\033[Word background color, font color m string \033[0m"  or
echo-e "\033[word background color; Font color M;ASCII code m string \033[0m string (can null) \ n " 
principle

Here is an example to illustrate:

Role:
42-> background color is white,
30-> font is black,
5-> font Flashing,
0-> Turn off all properties
The output character "David use Echo say", and then reset the screen to the default settings, the output character "Hello World" after the color back to normal

-E is an option for echo that activates the parser for special characters, which is the interpretation of the backslash escape character (that is, \). \033 Boot A sequence of unconventional characters. means setting the property and then ending the unconventional character sequence, where the effect character is
47;30;5 and 0. Modifying 47;30;5 can produce combinations of different colors, and the values are not related to the order in which they are encoded.

Group command:

Echo-e "\033[20;1h\033[1;4;32m David use echo say \033[0m Hello world \ n"

This line of command first \033[20;1h the cursor to the terminal 20th row 1th, followed by \033[1;4;32m the Text property to a highlighted, underlined and green color, and then output Hello,world, and finally \033[0m to set the Terminal property to default. This will not see the command prompt after the command is complete.

We can achieve complex control of terminal output through the combination of various commands. Range

0 Reset property to default setting
1 set bold
2 set half brightness (analog color display color)
4 set underline (simulate color of color display)
5 set flashing
7 Set reverse image
8 fade 
22 Set general density
24 off underline
25 off flashing
27 off reverse image

/font color range (foreground): 30~39
30: Black 
31: Red 
32: Green 
33: Yellow 
34: Blue 
35: Purple 
36: Dark green 
37: White 

38: Underline the default foreground color
39: Close the Underline

//Word background color range on the default foreground color (background color ): 40~49 
40: Black 
41: Crimson 
42: Green 
43: Yellow 
44: Blue 
45: Purple 
46: Dark green 
47: White 

NA cursor up n line 
NB cursor down n line 
nC cursor right move n line 
ND cursor left n line 
y;xh set cursor position 2J clear 
screen 
K clear from cursor to end of line 
s save cursor position 
u Restore cursor position 
? 25l hidden cursor 
? 25h display cursor
some colors
Echo-e "\033[0m None \033[0m" Echo-e "\033[30m black \033[0m" echo-e "\033[1;30m" Dark_gray \033[0m "echo-e" \033[0;34m " Blue \033[0m "Echo-e" \033[1;34m light_blue \033[0m "echo-e" \033[0;32m Green \033[0m "Echo-e" \033[1;32m Light_green \033[0m "Echo-e" \033[0;36m cyan \033[0m "echo-e" \033[1;36m Light_cyan \033[0m "echo-e" \033[0;31m red \033[0m "echo -E "\033[1;31m light_red \033[0m" echo-e "\033[0;35m purple \033[0m" Echo-e "\033[1;35m light_purple \033[0m" Echo-e "\ 033[0;33m Brown \033[0m "echo-e \033[1;33m yellow \033[0m" echo-e "\033[0;37m light_gray \033[0m" Echo-e "\033[1;37m W Hite \033[0m "Echo-e" \033[0m none \033[0m "Echo-e" \033[0m none \033[0m "Echo-e" \033[0m none \033[0m "Echo-e" \033[0  M none \033[0m "Echo-e" \033[0m none \033[0m "Echo-e" \033[40;37m black background white text \033[0m "Echo-e" \033[41;30m red Bottom black word \033[0m "echo -E "\033[42;34m green bottom Blue word \033[0m" echo-e "\033[43;34m yellow bottom Blue word \033[0m" echo-e "\033[44;30m Blue bottom Black word \033[0m" echo-e "\033[45;30m Purple bottom Black word \033[0m "echo-e" \033[46;30m Blue Bottom Black word \033[0m "echo-e" \033[47;34m white bottom Blue word \033[0m "echo-e" \033[4;31m underline the red word \033[0m "echo-e" \033[5;31m the Scarlet Letter is flashing \033[0m "Echo-e" \033[8m hidden \033[0m "

The effect chart is as follows:
extend A predefined color variable

Red_color= ' \e[1;31m '  
yelow_color= ' \e[1;33m ' blue_color= ' \e[1;34m ' reset= ' \e[0m '  
#需要使用echo-

E
echo-e  "${red_color}===david say RED color===${reset}"
echo-e  "${yelow_color}===david say Yelow Color===${reset} "
echo-e  " ${blue_color}===david say Green Color===${reset} "

The effect chart is as follows:

If there is a mistake, please note.

Email:dxmdxm1992#gmail.com

BLOG:CSDN Magicdavid.top

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.