Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I write:

  $ infocmp -1 xterm
        ...
    colors#8,
    bold=\E[1m,
    blink=\E[5m,
        ...
  $ tput -Txterm colors && echo OK || echo err
8
OK
  $ tput -Txterm blink && echo OK || echo err
^[[5mOK

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
ok

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\]'
  ;;
esac

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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!"
else
    echo "No tput or less than 8 colors, default to no coloring"
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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