1

I have a C program that runs

execvp("grep", args);

Where args is an array {"grep", "test"} (test being the word I want grep to find). The issue is that grep's output is not coloured. In a normal bash shell, grep highlights test in red, but in my program's output it just prints the line like this is a test with no highlighting or colours of any sort.

I'm also using execvp to execute some other commands which also produce coloured output in a bash shell, and have no colours in my output.

Is there any way to fix this? Something I need to do to stdout?

Roman
  • 235
  • 3
  • 14

1 Answers1

2

You need to provide the correct option to grep in order to get it to colorize. Most likely, your shell environment includes:

alias grep='grep --color=auto'

but execvp knows nothing about aliases.

So create the args array: {"grep", "--color=auto", "test", 0} and use that in your execvp("grep", args); call.

rici
  • 234,347
  • 28
  • 237
  • 341