Question

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?

Was it helpful?

Solution

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

grep -v "Dogs" list-of-animals | less

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.

OTHER TIPS

Use less -R or add LESS=-R to the environment. This requests that less pass some escape sequences (such as color) to the terminal instead of printing them as normal characters.

grep and friends detect whether output is to a terminal. When piped to less, it isn't, so they disable colouring.

Look at isatty to check whether the output is a terminal.

Note that I sometimes find this quite annoying because I want less to display the colors:

alias less='less -SR'
alias grep='grep --color=always'

Also look at ANSIFilter for the reverse: to filter ANSI escapes out of existing streams (you can also use it to produce HTML, RTF, and possibly other formats from them)

If you want to allow less to pass colour escape sequences to the terminal:

> echo -e "\033[31m -- Hello World! -- \033[m" | less -R

...or if you want to pass all escape sequences:

> echo -e "\033[31m -- Hello World! -- \033[m" | less -r

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top