如何改变回声在Linux中输出颜色 [英] How to change the output color of echo in Linux

查看:153
本文介绍了如何改变回声在Linux中输出颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试打印使用echo命令终端的文本。

I am trying to print a text in the terminal using echo command.

我想打印在红色的文字。我该怎么办呢?

I want to print the text in a red color. How can I do it?

推荐答案

您可以使用这些 ANSI转义codeS

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

然后在你的脚本中使用它们像这样:

And then use them like this in your script:

RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"

这版画红色。

从@詹姆斯Lim的评论,如果您使用的是回声命令,一定要使用-e标志,让反斜杠。

From @james-lim's comment, if you are using the echo command, be sure to use the -e flag to allow backslash escapes.

# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow\n"

这篇关于如何改变回声在Linux中输出颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆