Urgent.News

What's breaking now, across thousands of outlets.

Tech

FORCE_COLOR: Keep Your Command Line Colorful, Even when Piping - Retain Text Colors in Logs, Output Streams, and More

The NO_COLOR environmental variable allows command-line programs to output plain text. However, users often need a way to force color in certain situations, such as piping output through tools like less, grep, or tee. These actions can disable color output, causing frustration for users who rely on it. Additionally, some CI environments may disable color output by default or detect a non-interactive terminal, leading to color being disabled.

To address these issues, an informal standard was proposed in 2023. According to this standard, command-line software that outputs colored text should check for the presence of the FORCE_COLOR environment variable. If this variable is present and not an empty string, regardless of its value, the software should force the addition of ANSI color. This means that users can enable color in any context by setting the FORCE_COLOR variable, for example, by adding FORCE_COLOR=1 to their environment.

Software that supports the NO_COLOR variable but cannot force color should consider offering the FORCE_COLOR option. Those already providing a method to force colored output should also support the FORCE_COLOR variable. If a software developer adds FORCE_COLOR support, they should submit a pull request to add their software to the list of compliant programs. Developers are also encouraged to contribute to the standard by submitting a pull request.

While many users prefer to have color enabled, certain actions can disable it. Piping output through tools like less, grep, or tee, as well as being detected as a non-interactive terminal in CI environments, can all lead to color being disabled. To overcome this limitation, the FORCE_COLOR variable offers a consistent method to force enable color output, even in situations where other methods like --color or -c are not applicable.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at force-color.org →

More in Tech

More from Friday 4 September →