TERM environment variable not set. from clear inside a container
means the command ran through docker exec without a terminal, so the
command had no TERM value. Add -t to docker exec,
or pass one with -e TERM=xterm.
top fails in the same situation without saying why: it printed nothing and
returned 1. The same two fixes apply.
The error
docker exec fix10-lc-u3 clear
TERM environment variable not set.
And with top beside it, through bash -c:
docker exec fix10-lc-u3 bash -c 'clear 2>&1; echo exit=$?; top -n 1 2>&1 | head -3; echo exit=${PIPESTATUS[0]}'
TERM environment variable not set.
exit=1
exit=1
The second exit=1 is top's own exit code; it wrote no line at
all.
Why it happens
clear names the missing variable itself. Started without -t,
the command saw no value at all:
docker exec fix10-lc-u3 sh -c 'echo TERM=[$TERM]'
TERM=[]
With -t, Docker gave the command a terminal and the variable was set:
docker exec -t fix10-lc-u3 sh -c 'echo TERM=[$TERM]'
TERM=[xterm]
The fix
Run it with a terminal:
docker exec -t fix10-lc-u3 bash -c 'clear; echo exit=$?'
[H[2J[3Jexit=0
The output is the escape sequences clear sends, shown as captured except
that the unprintable escape byte before each bracket is left out; in a real terminal
they clear the screen. Or set the variable without a terminal:
docker exec -e TERM=xterm fix10-lc-u3 bash -c 'clear; echo exit=$?'
[H[2J[3Jexit=0
For top, add -b as well; with TERM set it printed
its summary lines:
docker exec -e TERM=xterm fix10-lc-u3 bash -c 'top -b -n 1 2>&1 | head -3; echo TERM=$TERM'
top - 05:55:41 up 43 min, 0 user, load average: 0.00, 0.06, 0.28
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
TERM=xterm
Which fix to use? -t gave the command a terminal and set
TERM to xterm, the same value the -e option sets
by hand, so for commands you type yourself -t is the shorter choice. The
-e form worked without a terminal, and top -b printed plain
summary lines in the test. In both fixed runs, clear returned 0 instead of
1, which is the value a script should check. Note that the message names the variable,
not the missing terminal, so it is easy to look for the problem in the container's
settings instead of in the docker exec line where it starts.
How it was reproduced
The capture was made in a container started with
docker run -d --name fix10-lc-u3 ubuntu:24.04 sleep infinity (Ubuntu 24.04.5
LTS, bash 5.2.21) on Docker Engine 29.9.3. Every docker exec line was sent
from Windows PowerShell 5.1 exactly as shown, with the sh -c and
bash -c text in single quotes so it reached the container unchanged. The
container name is the test's own.
Frequently asked
- How do I fix TERM environment variable not set in Docker?
- Add -t to docker exec, or pass -e TERM=xterm. In the test both made clear return 0.
- Why does top print nothing in docker exec?
- Without a terminal TERM was empty, and top -n 1 printed nothing and returned 1. With TERM=xterm and -b it printed its summary.
- What is TERM set to with docker exec -t?
- In the test echo printed TERM=[xterm] with -t and TERM=[] without it.
More decoded errors in the Fixes category.