I have a script that uses escape codes to highlight text matches in its output. All is good except when the output is piped to less, which prints the escape codes
e.g.
$ echo -e "\033[31m -- Hello World! -- \033[m"
-- Hello World! --
Piped to less:
$ echo -e "\033[31m -- Hello World! -- \033[m" | less
ESC[31m -- Hello World! -- ESC[m
I was wondering how other tools like ls, grep, etc are able to output in color yet it looks fine when piped to less?
Most of the tools you mention call the C function isatty() which determines whether the relevant file descriptor (in this case stdout) of the process is going to a terminal.
If the output is for a terminal they enable colour, highlighting, make beep noises or whatever other features they think a human user would derive value from. If there isn’t a terminal, they output raw text for the digestion of other tools.
When you write
The isatty() call from grep runs on the file descriptor leading to the pipe, not your terminal. So it returns zero, errno is set to EINVAL or ENOTTY and grep outputs raw text suitable for less.