I write:

  $ infocmp -1 xterm
  $ tput -Txterm colors && echo OK || echo err
  $ tput -Txterm blink && echo OK || echo err

So I can use tput to check retminal capabilities.

Is this soution portable? Are there any another solutions?

Or may be I can stuck with solution:

  $ [ $TERM = xterm ] && echo ok || echo err

One use is to set PS1 in .bashrc. Another for highliting output to take attention from sh script. I can use such solution:

PS1='bash# '
case "$TERM" in
  xterm*) PS1='\[\033[35m\]bash# \[\033[0m\]'

which uses only built-in command of bash (so efficient) and work for all my cases but unfortunately does not portable.


Using tput is portable to a large number of operating systems.

However checking the return code only tells you whether the platform's terminfo database has a definition of that capability in it's entry for 'xterm'.

The return code does not tell you whether the currently used 'xterm' client actually supports that capability.

Note that there are a great variety of software programs that claim to be 'xterm' compatible. It is certainly likely that not all of them support all 'xterm' features, especially considering that there have been several consecutive versions of the reference implementation of xterm released and probably some forks. The xterm maintainer says "None implements as many as half of xterm's controls."

So far as I know, none of the usual standards bodies (ANSI, ECMA, The Open Group) have published a standard that defines xterm. The current maintainer of xterm does also maintain a vt100 test program (vttest) to assist with his xterm work.

So, there's probably no way an application itself can make use of the TERM value to be sure that a terminal claimed to be an xterm actually supports a specific xterm feature. At least not for any arbitrary choice of xterm feature. If you involve a human (as I recall vttest did) you may have a better chance.

  • OK. Terminals can not say about its capabilities so $TERM ~= xterm* heuristics is pretty good. – gavenkoa Sep 7 '11 at 23:08

Yes, it is portable. How about this:

# BASH : Configure things differently if current terminal has >= 8 colors.
if which tput > /dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]]; then
    echo "Configure stuff to use colors here!"
    echo "No tput or less than 8 colors, default to no coloring"

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.