Let the echo output in the shell script be colored

Source: Internet
Author: User

There are two ways to let echo output a color

1. Define the color variable in the shell script and use ECHO-E to invoke the variable

2. When defining variables, define the ECHO-E into a variable, and then output the variable

Shell script that allows echo to display colors

#!/bin/bash
#定义颜色的变量
Red_color= ' \e[1;31m ' #红
Green_color= ' \e[1;32m ' #绿
Yelow_color= ' \e[1;33m ' #黄
Blue_color= ' \e[1;34m ' #蓝
pink= ' \e[1;35m ' #粉红
res= ' \e[0m '
#需要使用echo-E
Echo-e "${red_color}======red Color======${res}"
Echo-e "${yelow_color}======yelow Color======${res}"
Echo-e "${blue_color}======green Color======${res}"
Echo-e "${green_color}======green Color======${res}"
Echo-e "${pink}======pink Color======${res}"
echo "#############################################################"
#直接把echo-E is placed in the variable, directly output the variable when used
setcolor_success= "Echo-en \\033[1;32m"
Setcolor_failure= "Echo-en \\033[1;31m"
setcolor_warning= "Echo-en \\033[1;33m"
Setcolor_normal= "Echo-en \\033[0;39m"
echo----Oldboy trainning-----&& $SETCOLOR _success
echo----Oldboy trainning-----&& $SETCOLOR _failure
echo----Oldboy trainning-----&& $SETCOLOR _warning
echo----Oldboy trainning-----&& $SETCOLOR _normal

Script execution results are as follows


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.