Possible Duplicate:
grep loses coloring when run from bash script
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?
If there's no match, that should generally be considered a failure, so a return of 0 would not be appropriate. Indeed, grep returns 0 if it matches, and non-zero if it does not.
The quiet option ( -q ), causes grep to run silently and not generate any output. Instead, it runs the command and returns an exit status based on success or failure. The return status is 0 for success and nonzero for failure.
Looks like grep does not produce colors when not in interactive mode. You can force it to produce colored output:
grep --color=always $@
Grep has 3 color modes, Auto, Always, and Off.
Auto strips out the codes when it's connected to a non interactive output, such as a pipe (if you want to see why, try redirecting the output of grep --color=always
into a file and then look at the file.. control codes everywhere)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With