11

Could someone explain this? I'm using gnome-terminal. First, some information:

# echo $TERM
xterm

# infocmp xterm
Reconstructed via infocmp from file: /lib/terminfo/x/xterm
colors#8, cols#80, it#8, lines#24, pairs#64

# tput colors
8

I wrote simple script to create codes for extended set of colors:

# Output file (current directory) and text.
OFILE='xterm256_colors_test.sh'
OFILE_COLORS='terminal_256_colors'
OTEXT='Sed ut perspiciatis unde omnis iste natus error sit voluptatem...'

# Clearing the contents from previous runs.
if [ -e $OFILE ] || [ -e $OFILE_COLORS ]
then
  > $OFILE
  > $OFILE_COLORS
fi

# Bang!
echo -e '#!/bin/bash\n' | tee --append $OFILE $OFILE_COLORS &> /dev/null
echo -e "\necho -e \"" | tee --append $OFILE &> /dev/null

# \x1b is a control character changing behaviour of the shell.
# It is also the <Ctrl+V><Esc> sequence.
for i in {016..255}; do
  echo -e "\x1b[38;5;${i}m $OTEXT $i \x1b[0m" >> $OFILE
  echo -e "color${i}=\"\[\033[38;5;${i}m\]\"" >> $OFILE_COLORS
done

# End of echo.
echo '"' | tee --append $OFILE &> /dev/null

# The file should be executable.
chmod +x $OFILE

Despite the basic xterm terminal emulator when I run the generated script I can see all 240 colors. Why? I thought I should change $TERM to xterm-256color first.

18

The TERM environment variable is a way that you, the user, can tell programs (e.g., emacs, grep, less, ls, and vim) what kind of terminal they are running on, so they will know its parameters, including what capabilities it has and what escape sequences they need to issue to access them.  This exists because it’s too hard, in general, for the software to determine this for itself (and was pretty much impossible when users interfaced with computers through terminals that were external, and only connected to the computer by a data cable).

gnome-terminal is a program that provides terminal-like services to the user and the programs that the user runs within the terminal.  gnome-terminal may be aware of environment variables that were set in its environment, before it was invoked (DISPLAY being the obvious example), but it has no knowledge of environment variables that are set in in the processes that are running under it.

So, gnome-terminal has whatever capabilities it has.  It may be possible to adjust/constrain these externally, e.g., through command-line options, the pre-existing environment, configuration files, and dialogs in the window frame, but not by changing TERM in the shell in the window.  If it’s capable of displaying 256 colors, then it’s capable of displaying 256 colors, and you will be able to cause it to do so by sending it the appropriate escape sequences.  But, as long as you have TERM set to xterm, the programs that you run will believe that you are telling them that they are running in an eight-color-capable terminal, and so they will restrict their requests (escape sequences) to those capabilities.  You need to set TERM to xterm-256color, not to enable gnome-terminal to display 256 colors, but to tell programs like grep and ls to ask it to use more than 8 colors.

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.