2

Is there a way to detect if a program is piped into another one? Because when I print a colored string using ANSI colors, the get piped too, which I want to prevent. Or is there a better way to print colors?

Arwed Mett
  • 2,614
  • 1
  • 22
  • 35
  • Note that some of the colourizing programs provide an option like `—color=always` with options such as `auto` and `off` as alternatives. – Jonathan Leffler May 22 '18 at 21:08
  • Take a look at this: [https://stackoverflow.com/questions/6534556/how-to-remove-and-all-of-the-escape-sequences-in-a-file-using-linux-shell-sc](https://stackoverflow.com/questions/6534556/how-to-remove-and-all-of-the-escape-sequences-in-a-file-using-linux-shell-sc) – Leslie May 22 '18 at 21:45

1 Answers1

4

Some program do accept color when piped into ( like less -r ).

But in your case you would want to know if your current output is a tty, and use isatty for that purpose, and avoid printing color when your output is not a terminal.

You will also need to use fileno to convert your FILE* to a file descriptor.

dvhh
  • 4,724
  • 27
  • 33