Page 1 of 1

Colored output on the console without curses Rate Topic: -----

#1 GWatt  Icon User is offline

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Posted 05 December 2008 - 08:00 AM

Have you ever wanted to use color int your output, but didn't know how? Well here's your chance.
These example should work in almost any *nix terminal system. Linux, Mac, etc

type this into your terminal:
echo "^[[0;32;40mThis is in color"
NOTE: the ^[ is actually a special escape character you get by pressing ctrl v + esc. If you write a program in C or something, the value of the character is 27, 0x1b, or \033 if you want to embed it.
also, the quote marks are necessary for this to work

That should be in green, and on my computer the green carries through to every following line. If you want to not be working in terminal with green text, what you can do is type this:
echo "^[[0;;m"
and that should reset your terminal colors back to normal.

The anatomy of this is ^[[ attribute; foreground; backgroundm

The attributes are:
0 - reset everything
1 - bright (bold)
2 - dim
3 - underline (didn't for me, but some worked for some)
4 - underline (worked for me)
5 - blink
6 - nothing
7 - reverse (color scheme, not the text)
8 - hidden

The foreground colors are:
30 - black
31 - red
32 - green
33 - yellow
34 - blue
35 - magenta
36 - cyan
37 - white

and the background colors are the same as the foreground colors + 10
black - 40, etc

So, if you want to have cyan blinking text on a magnta background, you would use this:
echo "^[[5;36;45mFUGLY"

and because that is very ugly, let's go back to the default: echo "^[[0;;m"

What can you do with this? I don't know, have fun.
The first thing I did was set my terminal prompt string to be green.
export PS1="^[[0;32;40m\h:\W \u ^[[0;;m\$ "
if you want to make that permanent,
echo "export PS1=\"^[[0;32;40m\h:\W \u ^[[0;;m\$ \"" >> .bashrc
should do the trick.

for PS1:
\h - host name
\W - present working directory
\w - full path of PWD
\u - user name

Is This A Good Question/Topic? 0
  • +

Replies To: Colored output on the console

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 06 December 2008 - 07:27 AM

Interesting.
I always wondered how this was done in Linux.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Posted 13 December 2008 - 03:27 AM

hey thanks! i have always wondred how to do that in linux! now i know, thanks for your great post!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1