I use in my bash script the tput command in order to colored the text


tput setaf 2

when I run the script from putty or console every thing is ok

but when I run some external WIN application engine that run the script via SSH the we get the following error on tput

tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified

please advice what need to set ( ENV or else ) in my bash script in order to use the tput command ?

  • You need to set $TERM to whatever terminal emulator that application is compatible with.
    – phemmer
    Jan 10 '18 at 13:35
  • can you show me please example
    – yael
    Jan 10 '18 at 13:47

When connecting via ssh, environment variables may (or may not) be passed to the remote application. Also a "WIN application engine" could very well not set TERM at all.

If TERM is putty (or xterm, for that matter), these have the same effect:

tput setaf 2
tput -T putty setaf 2

since the control sequences used for setaf are the same. Likewise, if TERM is linux, these are the same

tput setaf 2
tput -T linux setaf 2

The setaf is used for setting the foreground (text) to a particular value using ANSI (x3.64) escape sequences. Most of the terminals you are using do that — or some do not recognize any of those escape sequences. Since the application was not mentioned, you will have to experiment to see if the "WIN application engine" recognizes those escape sequences. If it does, it probably uses the same ANSI escapes, so you could just do

tput -T xterm setaf 2

(There are other differences between putty, linux and xterm, of course).


Here are a couple of possible options.

  1. Don't use tput unless you've an interactive terminal session. You could choose either of these approaches:

    [[ -t 1 ]] && tput setaf 2 ...          # Only set a colour if we have a tty
    [[ -n "$TERM" ]] && tput setaf 2 ...    # Only set a colour if we have a terminal type definition
  2. Set a dummy terminal type. You can export TERM=dumb to disable most features of terminfo/termcap. Obviously you would want to execute this only if the terminal type was not already set:

    [[ -z "$TERM" ]] && export TERM=dumb    # Set a dummy terminal type if none set
    [[ ! -t 1 ]] && export TERM=dumb        # Set a dummy terminal type unless a tty

    With this approach you don't need to change any subsequent code; you could even set it before running your application and it wouldn't need to know any difference.


Use tty command to identify if your script is being executed from a terminal or remotely:

if tty -s ; then tput setaf 2 ; fi


This is my solution for the tput problem:

# when $TERM is empty (non-interactive shell), then expand tput with '-T xterm-256color'
[[ ${TERM}=="" ]] && TPUTTERM='-T xterm-256color' \
                  || TPUTTERM=''

declare -r    RES='tput${TPUTTERM} sgr0'       REV='tput${TPUTTERM} rev'
declare -r    fRD='tput${TPUTTERM} setaf 1'    bRD='tput${TPUTTERM} setab 1'
declare -r    fGN='tput${TPUTTERM} setaf 2'    bGN='tput${TPUTTERM} setab 2'
echo ${fRD}" RED Message: ${REV} This message is RED REVERSE. "${RES}
echo ${fGN}" GREEN Message: ${REV} This message is GREEN REVERSE. "${RES}

that way it makes no sense if there's an interactive or a non-interactive shell - tput still works fine.

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.