I have a Bash script that is being run interactively and as a cron
job. When run interactively it outputs colored text on the terminal. However, when run in cron
it doesn't have a terminal and therefore I get plenty of [1;31m
and similar in the output.
Is there a way in Bash to tell it to swallow the escape codes if the output device doesn't support them?
This is related to How to check if bash can print colors which is about checking the condition. However, I want to change as little as possible in the invocation of echo
or printf
while retaining the dual functionality of colors or not depending on the output device.