Do not send escape codesRespect My $TERM

Can you read ^[[1;32it^[[0m?

It ^[[1;31isn’t^[[0m easy! But it could be… All programs have to do is respect my TERM environment variable.

Users run command line programs in different kinds of display environments, some of which are “dumb”. When programs disrespect $TERM by outputting ANSI escape codes, we can’t read ^[[1;32it^[[0m.

Stop frustrating your users. Make sure your output is always legible by using tput in scripts and curses in your programs.

Output Respectfully

The rest of this page is dedicated to how to format program output while respecting $TERM.

Please contribute your favorite language examples:

Thank you for Respecting My Term. Tell your friends!

Apologies to non-POSIX command line users for using $TERM generically. Your terminal environments deserve respect, too!


Instead of embedding terminal escape codes, use tput to colorize output in scripts:

success() {
    tput setaf 2
    echo "$@"
    tput setaf 0
    return 0

success 'Hello, World!'

This function works in any POSIX compatible shell.

You can learn more about tput from the GNU Term Utils Manual or from the Linux Documentation Project.


Python supports curses out of the box:

import curses, sys

def success(s):
    colors = curses.tigetstr('setaf')

    def color(n):
        return curses.tparm(colors, n) if colors else ''

    sys.stdout.write(color(2) + s + color(0))

success('Hello, World!\n')


C has had curses for decades:

#include <stdarg.h>
#include <stdio.h>
#include <term.h>

void success(const char *fmt, ...) {
    va_list argp;
    char *colors = tigetstr("setaf");

    if (colors)
        printf("%s", tparm(colors, 2));
    va_start(argp, fmt);
    vprintf(fmt, argp);
    if (colors)
        printf("%s", tparm(colors, 0));

int main() {
    setupterm(NULL, 1, 0);
    success("Hello, World!\n");
    return 0;

Work arounds

List of commands and flags to turn off color with various tools:


npm config set color false
npm config set progress false

One last point…

Let your IDE colorize…