I am playing around with shell scripts that use ANSI codes and found that for one reason or another different escape codes are supported depending on your terminal/OS.

In some cases I get a dump of unparsed gunk unexpectedly, which I'm assuming means my terminal (on Mac OS) doesn't support that escape code used, despite having read in a number of places that these mean the same thing:

27 = 033 = 0x1b = ^[ = \e

In searching I found this question about detecting slash-escaped support.

The selected answer sniffs the $TERM value to detect

case $TERM in

But I wonder how reliable that is.

Is there a standard way to check for escape code support (primarily for Bash), or is that script pretty much the run of the mill?

  • Alternatively, what escape code can I use to 'guarantee' the most wide-spread support?
  • What about echo expansion -e?
  • What are general best practices in terms of portability/availability/distribution for scripts that use or reference control codes?

This is a nice read too for anyone else looking for info.

  • 4
    As mentioned in the documentation you reference: "It is generally not good practice to hard-wire terminal controls into programs. Linux supports a terminfo(5) database of terminal capabilities. Rather than emitting console escape sequences by hand, you will almost always want to use a terminfo-aware screen library or utility such as ncurses(3), tput(1), or reset(1)."
    – phemmer
    Oct 6, 2014 at 23:21
  • @Patrick - I've been trying a bunch of different things, and tput didnt work for me oob.
    – qodeninja
    Oct 6, 2014 at 23:37
  • 1
    There's no universal way of detecting all supported output sequences. If you're running under a true xterm however, you can query for termcap codes, see "Request Termcap/Terminfo String" in invisible-island.net/xterm/ctlseqs/ctlseqs.html
    – o11c
    Oct 7, 2014 at 0:01

2 Answers 2


Have you got specific operations in mind?

Here is an example of using standout mode, which on many terminals will give a strong visible result:

tput smso; echo hello, world; tput rmso

The tput tool uses the value of the $TERM environment variable to determine which escape sequences to output - if any. For example

( tput smso; echo hello, world; tput rmso ) | hexdump -C
00000000  1b 5b 37 6d 68 65 6c 6c  6f 2c 20 77 6f 72 6c 64  |.[7mhello, world|
00000010  0a 1b 5b 32 37 6d                                 |..[27m|

( tput smso; echo hello, world; tput rmso ) | hexdump -C
00000000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a           |hello, world.|

Interesting characteristic pairs can be found in man 5 terminfo, some of which are as follows:

  • Standout: smso and rmso
  • Underline: smul and rmul
  • Blink (yes!): blink
  • Doublewide: swidm and rwidm
  • Reverse: rev
  • Cancel all: sgr0

If you want to write bold text, but only if the terminal understands it, then a snippet like this will work

boldOn=$(tput smso)
boldOff=$(tput rmso)
# ...
printf "%s%s%s\n" "$boldOn" 'This message will be in bold, when available' "$boldOff"
  • (JFYI) The cat variant does show "standout" coloring in Konsole terminal in KDE Neon distro, so the pipe understands standout mode on my system. Unfortunately it's not clear to me how this can be used to detect the support of ANSI colour codes (ie. I don't find this answer to the original question), my use case is command line compiler tool - I want to decide in code if the error/warning output to stderr should contain ANSI colours by default. Seems reading the TERM envvar is cheap hack which will work in lot of scenarios well (like avoiding colours in windows cmd.exe)... but not "perfect".
    – Ped7g
    Mar 8, 2021 at 9:49
  • 1
    My phrase "a pipe isn't a device that understands standout mode" needs fixing. The example also doesn't fail in the way I described - perhaps that's new. The only thing that shuts up tput is TERM=dumb Mar 8, 2021 at 9:57

Actually it is possible to inquire DEC terminals (and their clones and emulations, including xterm) about their capabilities; just not about individual escape sequence support (or its completeness). UNIX generally doesn't use this feature, relying on termcap/terminfo databases (which document the quirks as well).

For reference, the sequences are DA ("Device Attributes", ANSI standard) and DECID ("Identify Terminal", DEC private).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .