I made a bash script producing colored output. The colors are fine when the script is called from an interactive shell. However, if the output is processed in another script, or passed through a pipe, it should not be colored (I think).
How is this handled usually? Should I provide an option to turn on/off the colored output, or is there a way to detect this automatically in the script? Preferably it works automatically and I only need a minimum of code to output colored / non-colored, depending on the script invocation...
One problem with automatic detection seems to be that a script always runs non-interactive, so I would have to know the interactive status of the parent shell instead.
ls
andgrep
they all offer a--color
switch of varying sorts, so I'd say add a switch. – slm Dec 29 '12 at 22:47