问题描述
我有一个简单的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 $ @
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脚本执行时会丢失彩色输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!