linux - 将 'grep --color=auto' 结果写入文件时颜色信息消失

标签 linux bash shell grep

当我使用别名 grep(grep --color=auto) 执行以下操作时

echo abcde | grep 'ab'

它返回 abcde(红色的 ab)。

但是

echo abcde | grep 'ab' >foo.txt

foo.txt 只有 abcde。

我猜我的终端在第一种情况下根据“grep”的一些标签显示红色的 ab,但 foo.txt 不包含它们。是 grep 的原因吗?

grep判断返回值应该是什么?

我的 grep 是 grep (GNU grep) 2.20

最佳答案

grep 识别您存储结果的位置,并在 auto 重定向设置的情况下禁用着色(颜色仅对终端启用)。

使用 --color=always 强制它使用它...总是,但我不认为你会发现那些控制序列很适合在文本文件中查看。

关于linux - 将 'grep --color=auto' 结果写入文件时颜色信息消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38498142/

相关文章:

python - 无需从终端打开的自动可执行 python 文件?

c# - Mono 中的 ADOMD 客户端

在 Windows 和 Linux 上编译

bash - Makefile错误消息自定义

bash - 出错时自动退出 Bash shell 脚本

shell - 从 shell 脚本中读取配置文件

linux - 将本地 repo 移动到远程单个文件

linux - bash 脚本 : how to get item name on a radiolist using dialog

shell - 为什么我没有得到我的函数的返回?

linux - 使用 shell 脚本从控制台添加多个值