改变echo的颜色-change the output color of echo in Linux

command line 改色

Posted by Yuankun Li on 2021-09-07
1
2
3
4
5
6
GREEN='\033[0;32m'
Yellow='\033[0;33m'
echo "${GREEN}"
echo "green string"
echo "${Yellow}"
echo "yellow string"

希望的输出:
“green string” 应该是绿色的
"yellow string" 应该是黄色的

实际输出:

1
2
3
4
\033[0;32m
green string
\033[0;33m
yellow string

if you are using the echo command, be sure to use the -e flag to allow backslash escapes.
所以我们应该把echo "${GREEN}" 改成 echo -e "${GREEN}"

1
2
3
4
5
6
GREEN='\033[0;32m'
Yellow='\033[0;33m'
echo -e "${GREEN}"
echo "green string"
echo -e "${Yellow}"
echo "yellow string"

实际输出:

1
2
green string
yellow string

green string 是绿色的
yellow string 是黄色的

参考链接



show git comment