是否可以通过 shell 重定向捕获彩色输出? [英] Can colorized output be captured via shell redirect?
问题描述
我使用的各种 bash 命令——花哨的差异、构建脚本等,产生大量的颜色输出.
Various bash commands I use -- fancy diffs, build scripts, etc, produce lots of color output.
当我将此输出重定向到一个文件,然后 cat
或 less
该文件之后,着色消失了——大概是 b/c 重定向的行为输出去除了告诉终端改变颜色的颜色代码.
When I redirect this output to a file, and then cat
or less
the file later, the colorization is gone -- presumably b/c the act of redirecting the output stripped out the color codes that tell the terminal to change colors.
有没有办法捕获彩色输出,包括着色?
Is there a way to capture colorized output, including the colorization?
推荐答案
捕获彩色输出的一种方法是使用 script
命令.运行 script
将启动一个 bash 会话,其中所有原始输出都被捕获到一个文件中(默认名为 typescript
).
One way to capture colorized output is with the script
command. Running script
will start a bash session where all of the raw output is captured to a file (named typescript
by default).
这篇关于是否可以通过 shell 重定向捕获彩色输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!