Why does grep lose color output when executed from a bash script?

Possible duplicate:
grep loses color when run from a 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 somehow does not match the actual launch of grep from the prompt directly, because the colors are missing from the results. When grep executed directly, the results show the file names in purple and correspond to red, but now all the output is the usual color of the terminal text. Can someone tell me how to get the color version from my script?

+4
source share
2 answers

It seems that grep does not create colors, if not interactively. You can force it to produce a color output:

 grep --color=always $@ 
+8
source

Grep has 3 color modes: Auto, Always, and Off.

Auto deletes codes when connected to a non-interactive output, for example, to a channel (if you want to understand why, try redirecting the output of grep --color=always to a file, and then look at the file .. control codes everywhere)

+6
source

Source: https://habr.com/ru/post/1432983/


All Articles