Strovent SystemsSign in
Reference

tput "No value for $TERM and no -T specified" and "TERM environment variable not set"

tput and clear need TERM to know your terminal. Cron jobs, services and ssh host command run without one. Guard the call, pass -T, or set TERM yourself.

tput and clear look up your terminal's codes in the terminfo database, using the name in the TERM environment variable. When TERM is not set at all, they stop with:

tput: No value for $TERM and no -T specified
TERM environment variable not set.

The first comes from tput (exit status 2), the second from clear (exit status 1). If TERM is set to a name the database does not have, you get tput: unknown terminal "name" instead (exit status 3); see TERM and terminfo.

Where TERM goes missing

  • Cron. Cron starts jobs with only a few variables, such as SHELL, LOGNAME and HOME. There is no TERM, and no terminal either.
  • systemd services. systemd sets TERM only for units connected to a terminal.
  • ssh host command. Without a terminal (no -t), sshd does not pass on a TERM. Scripts that run tput in a startup file then print the error on every ssh host command, and sometimes during scp or rsync.

Fixes

In each case there is no terminal to color or clear, so the cleanest fix is to skip tput:

if [ -t 1 ] && [ -n "$TERM" ]; then
  red=$(tput setaf 1)
  reset=$(tput sgr0)
fi

[ -t 1 ] is true only when output goes to a terminal. Other choices:

  • Name the terminal with -T, for example tput -T xterm-256color setaf 1, if you really want the codes in a log or file.
  • Set TERM in the job, for example TERM=xterm-256color at the top of the script.
  • Silence it by sending tput's error output to /dev/null. The variable is then empty when there is no terminal.
  • Ask for a terminal with ssh -t when you run an interactive command; see SSH "PTY allocation request failed on channel 0".

For a startup file such as .bashrc, put the tput lines after the check that returns early for non-interactive shells, so they never run for ssh host command.

Sources

Spotted a mistake? Email [email protected].