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,LOGNAMEandHOME. There is noTERM, and no terminal either. - systemd services. systemd sets
TERMonly for units connected to a terminal. ssh host command. Without a terminal (no-t), sshd does not pass on aTERM. Scripts that runtputin a startup file then print the error on everyssh 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 exampletput -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-256colorat 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 -twhen 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.
Related
- TERM and terminfo
- Unknown terminal type or "Error opening terminal" when you use tmux, screen or SSH
- Do all terminals understand ANSI escape codes? TERM, terminfo and dumb terminals
- tmux "open terminal failed: not a terminal" and bash "no job control in this shell" over SSH
- SSH "PTY allocation request failed on channel 0"