Use the echo command in shell to output color text

Source: Internet
Author: User
Tags echo command

First, familiarize yourself with the echo command

Name
Echo-display a line of text

Summary
Echo [Option]... [String]...

Description
Outputs a string to the standard output.

-N does not output line breaks

-E open the backslash (ESC) Escape

-E cancel the backslash ESC escape (default)

-- Help: Display help

-- Display version

That's all.
Note that the-e option is required for the output color.

Next, let's focus on the application of the ANSI control code in the output of color text.

-E is used to enable escape in Echo.
/E or/033 to output the ESC symbol
Set the color format:/E [background color; foreground color; Highlight m
Restore the default value:/E [0 m
The background color can be replaced by the following numbers:
0 transparent (Use Terminal color), 40 black, 41 red, 42 green, 43 yellow, 44 Blue 45 purple, 46 green, 47 white (Gray)

The foreground color (the color of the text) can be replaced by the following numbers.
30 Black 31 red, 32 green, 33 yellow, 34 blue, 35 purple, 36 green, 37 white (Gray)

Highlight is 1, not 0
Note that M follows the string.

For example
Lonelycorn @ untbook :~ $ Echo-e '/033 [0; 33; 1 mabc/033 [0m'
Output
ABC

Then we will give several powerful script Functions
1 cfont
# By zuoyang http://hi.baidu.com/test/
Cfont ()
{
While ($ #! = 0 ))
Do
Case $1 in
-B)
Echo-ne "";
;;
-T)
Echo-ne "/t ";
;;
-N) echo-ne "/N ";
;;
-Black)
Echo-ne "/033 [30 m ";
;;
-Red)
Echo-ne "/033 [31 m ";
;;
-Green)
Echo-ne "/033 [32 m ";
;;
-Yellow)
Echo-ne "/033 [33 m ";
;;
-Blue)
Echo-ne "/033 [34 m ";
;;
-Purple)
Echo-ne "/033 [35 m ";
;;
-Cyan)
Echo-ne "/033 [36 m ";
;;
-White |-gray) echo-ne "/033 [37 m ";
;;
-Reset)
Echo-ne "/033 [0 m ";
;;
-H |-help | -- Help)
Echo "Usage: cfont-color1 message1-color2 message2 ...";
Echo "eg: cfont-red [-blue message1 message2-red]";
;;
*)
Echo-ne "$1"
;;
Esac
Shift
Done
}
The usage is as follows:
Cfont-color string...
For example:
Cfont-cyan ABC
Note that the-n option must be added for line breaks during use. To restore the default value, add the-reset option.

2 color code table
#! /Bin/bash
T = 'gyw' # The test text

Echo
Echo "Default 40 M 41 m 42 m 43 m 44 m 45 m 46 m
47 M"
# FGS indicates the foreground color and BG indicates the background color.
For FGS in 'M' '1m' '30m' 1; 30m' '31m' 1; 31m' '32m' 1; 32m''
33m'' 1; 33m'' 34m'' 1; 34m'' 35m'' 1; 35m'' 36m'' 1; 36m'' 37m'' 1; 37m'
Do
Fg = $ (echo $ FGS | tr-D '')
Echo-en "$ FGS/033 [$ FG $ t"
For BG in 40 M 41 m 42 m 43 m 44 m 45 m 46 m 47 m;
Do
Echo-en "/033 [$ fg/033 [$ BG $ t/033 [0 m"
Done
Echo
Done
Echo

No, that's it.

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.