为什么 grep 从 bash 脚本执行时会丢失彩色输出? [英] Why does grep lose colored output when executed from a bash script?

查看:23
本文介绍了为什么 grep 从 bash 脚本执行时会丢失彩色输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
grep 在从 bash 脚本运行时丢失颜色

我有一个简单的 bash 脚本来在我的 grep 结果顶部打印标题:

I have a simple bash script to print a header on top of my grep results:

#!/bin/bash

for var in "$@"
do
    if [[ $var != -* ]];
    then
        break 
    fi
done

echo
echo -en "e[1;31m     ====== GREP $var ======e[0m
"
echo

grep $@

但最终的命令在某种程度上与直接从提示中实际运行 grep 不同,因为结果中缺少颜色.直接执行 grep 时,结果显示紫色的文件名和红色的匹配,但现在所有输出都是正常的终端文本颜色.有人能告诉我如何从我的脚本中获取彩色版本吗?

But the final command is somehow not the same as actually running grep from the prompt directly, because the colors are missing from the results. When executing grep directly, the results show filenames in purple and matches in red, but now all the output is the normal terminal text color. Can someone tell me how to get the colored version from my script?

推荐答案

看起来 grep 在非交互模式下不会产生颜色.您可以强制它产生彩色输出:

Looks like grep does not produce colors when not in interactive mode. You can force it to produce colored output:

grep --color=always $@

这篇关于为什么 grep 从 bash 脚本执行时会丢失彩色输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆