我写了一个简单的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
Run Code Online (Sandbox Code Playgroud)
Fre*_*Foo 56
你可能已经定义grep为的别名grep --color=auto在你.bashrc,但是这不加载脚本.grep --color在脚本中使用explicit .
当您运行脚本时,会生成一个新的 shell 来执行此操作。这个新环境与您的默认 shell 没有相同的设置。至于如何恢复着色,我不确定。您可以尝试在脚本开头获取您的个人资料:
#!/bin/bash
source $HOME/.bash_profile
Run Code Online (Sandbox Code Playgroud)
或者任何对您的特定 unix 风格有意义的文件(.profile、.bash_rc、.bashrc .bash_profile)等等。