在 printf 中使用颜色 [英] Using colors with printf

查看:14
本文介绍了在 printf 中使用颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当这样写时,它输出蓝色文本:

When written like this, it outputs text in blue:

printf "e[1;34mThis is a blue text.e[0m"

但我想在 printf 中定义格式:

But I want to have format defined in printf:

printf '%-6s' "This is text"

现在我尝试了几种添加颜色的方法,但都没有成功:

Now I have tried several options how to add color, with no success:

printf '%-6s' "e[1;34mThis is texte[0m"

我什至尝试将属性代码添加到格式中,但没有成功.这不起作用,我无法在任何地方找到将颜色添加到 printf 的示例,它定义了我的格式.

I even tried to add attribute code to format with no success. This does not work and I can't find anywhere an example, where colors are added to printf, which has defined format as in my case.

推荐答案

您将各个部分混合在一起,而不是将它们干净地分开.

You're mixing the parts together instead of separating them cleanly.

printf 'e[1;34m%-6se[m' "This is text"

基本上,将固定的东西放在格式中,将可变的东西放在参数中.

Basically, put the fixed stuff in the format and the variable stuff in the parameters.

这篇关于在 printf 中使用颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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