[brep从bash脚本运行时失去颜色

问题描述 投票:39回答:3

我写了一个简单的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
bash colors grep
3个回答
60
投票

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


3
投票

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

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

或在您的特定Unix风格(.profile,.bash_rc,.bashrc .bash_profile)中有意义的任何文件。


0
投票

除了pdem的注释,这也适用:#!/usr/bin/env -S bash -i

© www.soinside.com 2019 - 2024. All rights reserved.