What happens if you pipe `ls --color=always`?

QuestionsQuestions8 SkillsProDec, 16 2025
092

That's a very insightful question, and it directly relates to the third point we just discussed about --color=always!

If you pipe ls --color=always to another command, such as cowsay, here's what happens:

When you run:

ls --color=always | cowsay
  1. ls --color=always
    The ls command is executed with the --color=always option. This forces ls to include ANSI escape codes for coloring in its output, even though its output is being piped to another command (cowsay).

  2. The Pipe (|)
    The output (which now contains color codes) from ls is sent directly as input to the cowsay command.

  3. cowsay
    The cowsay command receives this input, including all the color codes. However, cowsay is designed to simply wrap the text it receives inside its ASCII art cow bubble. It doesn't interpret or display ANSI color codes; it treats them as just plain text characters.

Result:

You will likely see the text inside the cowsay bubble, but it will also contain visible, uninterpreted ANSI escape codes. These codes look like sequences starting with ^[[ or \x1b[ followed by numbers and letters (e.g., ^[[0m, ^[[01;34m). These codes are what your terminal normally interprets to display colors, but cowsay doesn't do that.

Example of Potential Output:

 __________________________________________________________________
/ ^[[0m^[[00mdata_file.txt^[[0m  ^[[01;34mtest^[[0m  ^[[00mtest_file.txt^[[0m \
\                                                                  /
 ------------------------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

(The exact codes might vary slightly, but the pattern will be similar.)

This demonstrates an important concept in Linux: not all programs that receive input are designed to interpret every kind of data. cowsay expects plain text, and when it gets plain text plus color codes, it just prints everything it receives. This is why --color=auto is often the better default for pipes, as it avoids sending unnecessary (and often problematic) color codes to programs that don't know what to do with them.

Feel free to try it out in your LabEx terminal:

ls --color=always | cowsay

You'll see those raw color codes appear, which is a great way to understand how they work! Let me know what you observe!

0 Comments

no data
Be the first to share your comment!