从 bash 脚本运行时 grep 失去颜色 [英] grep loses coloring when run from bash script
问题描述
我编写了一个简单的 bash 脚本,因为我反复使用具有相同参数的 grep 命令.我从 ~/bin 运行它,它运行得很好.
I wrote a simple bash script because I was using a grep command with the same arguments, repeatedly. I'm running it from ~/bin and it runs just fine.
我的问题是:当它通过我的 bash 脚本运行时,所有的颜色都消失了.完全相同的命令放在命令行中很好地对行号、文件名等进行了颜色编码.
My problem is: All the coloring is gone when it's run through my bash script. The exact same command put right into the commandline nicely color codes line numbers, filenames, etc.
这是我的 bash 脚本
Here's my bash script
#!/bin/bash
# grep php files inside of myfolder, recursively and with line numbers
grep -rn --include="*.php" "$2" /home/me/myfolder/$1
推荐答案
您可能已经将 grep
定义为 grep --color=auto
的别名 grep --color=auto
code>.bashrc,但这不是由脚本加载的.在脚本中使用显式 grep --color
.
You've probably defined grep
as an alias for grep --color=auto
in your .bashrc
, but that's not loaded by scripts. Use an explicit grep --color
in your script.
这篇关于从 bash 脚本运行时 grep 失去颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!