bash - 颜色转义代码

发布于 2024-12-17 11:00:58 字数 429 浏览 4 评论 0原文

我有一个脚本,它使用转义码来突出显示其输出中的文本匹配项。一切都很好,除了当输出通过管道传输到 less 时,它会打印转义码,

例如

$ echo -e "\033[31m -- Hello World! -- \033[m"
 -- Hello World! --

通过管道传输到 less

$ echo -e "\033[31m -- Hello World! -- \033[m" | less
ESC[31m -- Hello World! -- ESC[m

我想知道 lsgrep 等其他工具如何能够以颜色输出,但在通过管道传输到时看起来还不错

I have a script that uses escape codes to highlight text matches in its output. All is good except when the output is piped to less, which prints the escape codes

e.g.

$ echo -e "\033[31m -- Hello World! -- \033[m"
 -- Hello World! --

Piped to less:

$ echo -e "\033[31m -- Hello World! -- \033[m" | less
ESC[31m -- Hello World! -- ESC[m

I was wondering how other tools like ls, grep, etc are able to output in color yet it looks fine when piped to less?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺孤伤于心 2024-12-24 11:00:58

使用 less -R 或将 LESS=-R 添加到环境中。这要求 less 将一些转义序列(例如颜色)传递到终端,而不是将它们打印为普通字符。

Use less -R or add LESS=-R to the environment. This requests that less pass some escape sequences (such as color) to the terminal instead of printing them as normal characters.

只涨不跌 2024-12-24 11:00:58

grep 和朋友检测输出是否到终端。当通过管道传输到 less 时,情况并非如此,因此它们禁用着色。

查看 istty 来检查输出是否是终端。

请注意,我有时觉得这很烦人,因为想要更少地显示颜色

alias less='less -SR'
alias grep='grep --color=always'

另请参阅ANSIFilter 相反:从现有流中过滤 ANSI 转义(您还可以使用它来生成 HTML、RTF 以及可能的其他格式) )

grep and friends detect whether output is to a terminal. When piped to less, it isn't, so they disable colouring.

Look at isatty to check whether the output is a terminal.

Note that I sometimes find this quite annoying because I want less to display the colors:

alias less='less -SR'
alias grep='grep --color=always'

Also look at ANSIFilter for the reverse: to filter ANSI escapes out of existing streams (you can also use it to produce HTML, RTF, and possibly other formats from them)

爱要勇敢去追 2024-12-24 11:00:58

您提到的大多数工具都会调用 C 函数 isatty(),它确定进程的相关文件描述符(在本例中为 stdout)是否将发送到终端。

如果输出是针对终端的,他们会启用颜色、突出显示、发出蜂鸣声或他们认为人类用户可以从中获得价值的任何其他功能。如果没有终端,它们会输出原始文本以供其他工具消化。

当您编写时

grep -v "Dogs" list-of-animals | less

grep 的 isatty() 调用在通向管道的文件描述符上运行,而不是在您的终端上运行。因此它返回零,errno 设置为 EINVAL 或 ENOTTY,并且 grep 输出适合 less 的原始文本。

Most of the tools you mention call the C function isatty() which determines whether the relevant file descriptor (in this case stdout) of the process is going to a terminal.

If the output is for a terminal they enable colour, highlighting, make beep noises or whatever other features they think a human user would derive value from. If there isn't a terminal, they output raw text for the digestion of other tools.

When you write

grep -v "Dogs" list-of-animals | less

The isatty() call from grep runs on the file descriptor leading to the pipe, not your terminal. So it returns zero, errno is set to EINVAL or ENOTTY and grep outputs raw text suitable for less.

氛圍 2024-12-24 11:00:58

如果您希望允许 less 将颜色转义序列传递到终端:

> echo -e "\033[31m -- 你好世界!-- \033[m" | less -R

...或者如果您想传递所有转义序列:

> echo -e "\033[31m -- 你好世界!-- \033[m" |少-r

If you want to allow less to pass colour escape sequences to the terminal:

> echo -e "\033[31m -- Hello World! -- \033[m" | less -R

...or if you want to pass all escape sequences:

> echo -e "\033[31m -- Hello World! -- \033[m" | less -r

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文