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

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

问题描述


可能存在重复:


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



<$ p $如果[[$ var!= - *]];









$ b $
然后
break
fi
完成

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

echo

grep $ @

但是最终的命令与直接在提示符下实际运行 grep 不同,因为颜色从结果。当直接执行 grep 时,结果显示的文件名为紫色,匹配为红色,但现在所有输出都是正常的终端文本颜色。有人可以告诉我如何从我的脚本中获取彩色版本吗?

解决方案

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

  grep --color = always $ @ 


Possible Duplicate:
grep loses coloring when run from bash script

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\n"
echo

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?

解决方案

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天全站免登陆