bash - 颜色转义代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8220256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash - color escapes codes
提问by armandino
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
我有一个脚本,它使用转义码来突出显示其输出中的文本匹配。一切都很好,除非输出通过管道传输到less,这会打印转义码
e.g.
例如
$ echo -e "3[31m -- Hello World! -- 3[m"
-- Hello World! --
Piped to less:
输送到less:
$ echo -e "3[31m -- Hello World! -- 3[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?
我想知道诸如ls、grep等其他工具如何能够以彩色输出但在通过管道传输时看起来很好less?
采纳答案by tialaramex
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.
您提到的大多数工具都调用 C 函数 isatty() ,该函数确定进程的相关文件描述符(在本例中为 stdout)是否进入终端。
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.
来自 grep 的 isatty() 调用在通向管道的文件描述符上运行,而不是在您的终端上运行。所以它返回零,errno 设置为 EINVAL 或 ENOTTY,grep 输出适合 less 的原始文本。
回答by ephemient
Use less -Ror add LESS=-Rto the environment. This requests that lesspass some escape sequences (such as color) to the terminal instead of printing them as normal characters.
使用less -R或添加LESS=-R到环境中。这请求less将一些转义序列(例如颜色)传递给终端,而不是将它们打印为普通字符。
回答by sehe
grep and friends detect whether output is to a terminal. When piped to less, it isn't, so they disable colouring.
grep 和朋友检测是否输出到终端。当管道减少时,它不是,所以他们禁用着色。
Look at isattyto check whether the output is a terminal.
查看isatty以检查输出是否为终端。
Note that I sometimes find this quite annoying because I wantless to display the colors:
请注意,我有时会觉得这很烦人,因为我不想显示颜色:
alias less='less -SR'
alias grep='grep --color=always'
Also look at ANSIFilterfor 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)
另请查看ANSIFilter的相反情况:从现有流中过滤 ANSI 转义符(您也可以使用它来生成 HTML、RTF 和可能的其他格式)
回答by Ben James
If you want to allow less to pass colour escape sequences to the terminal:
如果要允许 less 将颜色转义序列传递给终端:
> echo -e "\033[31m -- Hello World! -- \033[m" | less -R
> 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
> echo -e "\033[31m -- Hello World! -- \033[m" | less -r

