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)." – Patrick Oct 6 '14 at 23:21
  • @Patrick - I've been trying a bunch of different things, and tput didnt work for me oob. – qodeninja Oct 6 '14 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 '14 at 0:01

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

If you were to pipe the sequence to, say, cat, the highlighting would become an empty operation because a pipe isn't a device that understands standout mode:

( tput smso; echo hello, world; tput rmso ) | cat

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
|improve this answer|||||

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).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.