grep从bash脚本运行时失去颜色

时间:2012-03-03 23:45:01

标签: bash colors grep

我编写了一个简单的bash脚本,因为我使用的grep命令重复使用相同的参数。我是从〜/ bin运行的,它运行得很好。

我的问题是:当我的bash脚本运行时,所有颜色都消失了。完全相同的命令直接放入命令行中,颜色代码行号,文件名等。

这是我的bash脚本

#!/bin/bash
# grep php files inside of myfolder, recursively and with line numbers

grep -rn --include="*.php" "$2" /home/me/myfolder/$1

2 个答案:

答案 0 :(得分:55)

您可能已在grep中将grep --color=auto定义为.bashrc的别名,但脚本未加载该grep --color。在脚本中使用明确的{{1}}。

答案 1 :(得分:1)

当您运行脚本时,会生成一个新的shell来执行此操作。此新环境与默认shell的设置不同。至于如何重新着色,我不确定。您可以尝试在脚本开头寻找您的个人资料:

#!/bin/bash
source $HOME/.bash_profile

或者对你的特定unix风格(.profile,.bash_rc,.bashrc .bash_profile)有意义的文件,仅举几例。

相关问题