Read the current text color in a xterm

Rather than using obfuscated escape sequences, use the tput facility instead. Here is an excerpt from my ~/.bashrc that I use for my PS1 prompt:

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

To reset the color information such that subsequent text is in the normal terminal color you would append ${NORMAL} to the end like so:

echo "${RED}this is red ${NORMAL}this is normal"

Leave a Comment