Skip to content

Instantly share code, notes, and snippets.

@mhelsley
Created June 9, 2022 03:30
Show Gist options
  • Save mhelsley/538aef29138a68ff23b91c74fc092213 to your computer and use it in GitHub Desktop.
Save mhelsley/538aef29138a68ff23b91c74fc092213 to your computer and use it in GitHub Desktop.
Preserve CLI color output

Many commands determine whether to color their output using escape sequences by checking if their output file descriptor is a terminal.

This doesn't work very well when piping that output into a pager like more or less.

(Ideally UNIX folks would have created a way to determine if executables in a pipeline preserve color escape sequences and if the output fd at the very end of a pipeline was a tty or not. But they didn't.)

So many commands have an explicit command line option that can be used to specify that the color output should turned on, off, or be switched based on the immedita-output-fd type.

This is a collection of the common ones on a typical Linux (especially the GNU-centric distros) system.

Command Color Option Environment Variable
dmesg --color=always
git [diff|log|show|status] --color=always
gcc -fdiagnostics-color=always GCC_COLORS (SGR)
grep --color=always GREP_COLORS (SGR)
ls --color=always LS_COLORS (See: dircolors)
systemctl `` SYSTEMD_COLORS (boolean) SYSTEMD_LOG_COLOR (boolean)
tree -C
`` `` ``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment