向前猛击颜色
有没有办法导出命令输出的颜色?
我们用一个小例子来解释一下:
ls -alh --color=auto
会打印目录的彩色内容,而
ls -alh --color=auto | cat
不会打印某些颜色。我想知道的是一个技巧或工具,我们称之为magic
,它可以恢复这些命令,例如\033[1m
,以便颜色可用于后续处理:
ls -alh --color=auto | magic | cat
或
ls -alh --color=auto | magic >> file
更新:
我仅在本示例中使用 ls ,但想知道是否存在一般可能性。
Is there any way to export the colors of an output of a command?
Let's explain it with a small example:
ls -alh --color=auto
will print the colored contents of the directory, while
ls -alh --color=auto | cat
won't print some color. What I want to know is a trick or a tool, let's call it magic
, that restores these commands like \033[1m
, so that colors are available for latter processing:
ls -alh --color=auto | magic | cat
or
ls -alh --color=auto | magic >> file
Update:
I'm using ls
just for this example, but want to know whether there is a general possibility.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会成功的。例如:
will do the trick. e.g:
由于颜色代码实际上是 ls 输出的一部分,因此无法“恢复”它们(因为它们一开始就不存在)。
但如果您使用
ls --color=always
,即使在非交互模式下使用,ls
也会输出颜色代码。Since the color codes are actually part of
ls
output, there is no way to "restore" them (since they are not there in the first place).But if you use
ls --color=always
,ls
will output color codes even when used in non-interactive mode.基本上,
ls
很智能,可以检测输出何时不发送到终端。如果您想让它不那么重要,请尝试ls --color=always
。Basically,
ls
is being smart, and detecting when the output isn't going to a terminal. If you want to tell it to be less so, tryls --color=always
.