Saturday, April 20, 2024
65
rated 0 times [  65] [ 0]  / answers: 1 / hits: 40109  / 2 Years ago, mon, february 28, 2022, 1:48:16

When I run a program (for example grep or ls) without a pager, its output is colored. However when I run it piping its output to less, no colors are shown.



For example, this command outputs colored output:



grep -r something


but this doesn't:



grep -r something | less


Why? How can I see colors through less?


More From » command-line

 Answers
1

There are two problems here:




  • Commands like ls —which auto-detect the colour support— don't find support from pipes

  • less is set to just display colour codes by default.



Both can be overcome but it's a bit clunky:



ls --color=always | less -R


This isn't ls specific. Many commands that support colour also have an override argument.






A slightly more in-depth answer is that ls is checking whether or not its STDOUT belongs to a real terminal or not. When you pipe things around, the STDOUT is set to the STDIN of the next command.



You can see this at work in the ls source code. It's using the isatty command (a core POSIX interface) to work out what the situation is:




  • Are colours on by default:



        print_with_color = (i == color_always
    || (i == color_if_tty
    && isatty (STDOUT_FILENO)));

  • Do we try to output in multiple columns:



    if (format == long_format)
    format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);

    //...

    if (isatty (STDOUT_FILENO))
    {
    format = many_per_line;
    set_quoting_style (NULL, shell_escape_quoting_style);
    qmark_funny_chars = true;
    }
    else
    {
    format = one_per_line;
    qmark_funny_chars = false;
    }



grep does a very similar thing, unless explicitly overridden, it'll detect colour support, with isatty:



color_option = isatty (STDOUT_FILENO) && should_colorize ();

[#24657] Tuesday, March 1, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ofunn

Total Points: 164
Total Questions: 116
Total Answers: 116

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;