How to change font colors in terminal?

31,208

Solution 1

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)

Solution 2

As Olivier Dulac said, you need ANSI escape codes, please refer to Bash tips: Colors and formatting

for details.

Share:
31,208
blazonix
Author by

blazonix

Fluent in Java, C and currently engaged in simple website projects to acquire PHP, JavaScript, MySQL, jQuery and JSON... Programming is amazing. It makes people smarter. It made iPhone and Galaxy Note possible. It changed (and is still changing) the world. As it happens, I'll probably be spending many days and nights on this forum answering (if I can) and asking questions to sharpen my saw...so looking forward to hanging around here and meeting with y'all awesome tribe members! (What's the English colloquial equivalent of よろしくお願いします??) Apart from programming, also a marketer, linguist and enthusiastic musician.

Updated on September 18, 2022

Comments

  • blazonix
    blazonix over 1 year

    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?

    • Admin
      Admin almost 10 years
      who you gonna call...
    • Admin
      Admin almost 10 years
      @mikeserv I'm sorry, what does that mean?
    • Admin
      Admin almost 10 years
      Oh, sorry. Nothing really, but I was just thinking you might be Peter Venkman. But if you were you would have immediately responded... Ghostbusters... Still, I checked out the sidebar about related questions and I found a pretty awesome answer to something like this that included a bunch of other awesome links. Specifically you'll need to use terminal escapes - or possibly tput - in your $PS1 variable - or maybe in $PROMPT_COMMAND. Play around with it. unix.stackexchange.com/questions/43075/…
    • Admin
      Admin almost 10 years
  • Stephen Rauch
    Stephen Rauch about 7 years
    This maybe a start to answering the question. But as of now ... Can you please edit this to edit it into an answer?