How to change font colors in terminal

colorsescape-charactersterminal

I know how to change the font color via preferences, but it changes the color of ALL text, as in below:

   what I have

What I'm going for is something more like this:

   what I want

Any tips?

Best Answer

A lot of unix terminals can recognise some (but usually not all) Ansi Escape codes

So you can use those (the ones that work for your terminal) to change the display as needed.

A very simple example (I use printf as it's portable across many different (and old!) shells) :

export _norm_="$(printf '\033[0m')" #reset "everything" to normal
export _bold_="$(printf '\033[1m')"   #set bold
export _rred_="$(printf '\033[0;1;5;31m')" #"reverse red"
echo "This is an ${_rred_}ERROR${_norm_} and this is ${_bold_}A WARNING${_norm_}"

Please not that those may vary depending on the terminal type (TERM=...), and the program you connect to that machine with (most notably: reverse can become "blink" when using some windows terminal such as F-secure instead of Putty, for example..)

In other words: this is not completely portable, and depends on many things. But "bold" is quite always working. "reverse" is more prone to be terminal dependant.

tput is also better to use as it's taking care to find out the proper sequence for your terminal, but it's not always available (not on old machines, for example)

Related Question