I have the following piece of code.
bold=''
reset=$(echo -en '\033[0m')
black=$(echo -en '\e[1;30m')
magenta=$(echo -en '\033[00;35m')
blue=$(echo -en '\e[1;34m')
cyan=$(echo -en '\e[1;36m')
green=$(echo -en '\e[1;32m')
orange=$(echo -en '\e[1;33m')
purple=$(echo -en '\e[1;35m')
red=$(echo -en '\e[1;31m')
white=$(echo -en '\e[1;37m')
yellow=$(echo -en '\e[1;33m')
lime_yellow=$(echo -en '\e[1;33m')
power_blue=$(echo -en '\e[1;34m')
blink=$(echo -en '\e[1;31m')
reverse=$(echo -en '\e[1;31m')
underline=$(echo -en '\e[1;31m')
if [ -x /usr/bin/tput ] && tput setaf 1 &>/dev/null; then
echo "tput color is supported."
tput sgr0 # Reset colors
bold=$(tput bold)
reset=$(tput sgr0)
black=$(tput setaf 0)
magenta=$(tput setaf 5)
blue=$(tput setaf 33)
cyan=$(tput setaf 37)
green=$(tput setaf 64)
orange=$(tput setaf 166)
purple=$(tput setaf 125)
red=$(tput setaf 124)
white=$(tput setaf 15)
yellow=$(tput setaf 136)
lime_yellow=$(tput setaf 190)
power_blue=$(tput setaf 153)
blink=$(tput blink)
reverse=$(tput smso)
underline=$(tput smul)
else
echo "tput color is not supported. Use old school colors."
fi
echo ${red}RED${green}GREEN${yellow}YELLOW${blue}BLUE${purple}PURPLE${cyan}CYAN${white}WHITE${reset}
Basically there are two types of colors, tput
generated or the old-fashioned escape characters like \e[1;32m
. Since the tput
type is more interesting, for example, it supports blinking and underline, the code uses tput
type color if possible. Here is an image to prove it works as expected in Oracle Linux 7.6 (kinda like RedHat or CentOS) GUI environment.
When I run it from other terminals, it doesn't work. For example, below is the snapshot when running in MobaXterm.
I also tried putty
and it doesn't work either. Is there anything wrong with my code?
Update
I executed echo $TERM
in each of the terminals and below is the result.
Oracle Linux with Desktop environment (color works) Output: xterm-256colorMobaXterm on Windows (color doesn't work) Output: xterm
putty on Windows (color doesn't work) Output: xterm
$TERM
in the non-working cases? – Mark Plotnick Jan 08 '20 at 11:29$TERM
. – Just a learner Jan 08 '20 at 11:42tput
. This is a misconception. Indeed,tput
for many terminal types retrieves similar escape sequences. – JdeBP Jan 08 '20 at 12:58