School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,008 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,038 people online right now. Registration is fast and FREE... Join Now!




Colored output on the console

 
Reply to this topicStart new topic

> Colored output on the console, without curses

GWatt
Group Icon



post 5 Dec, 2008 - 07:00 AM
Post #1


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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 6 Dec, 2008 - 06:27 AM
Post #2
Interesting.
I always wondered how this was done in Linux.
Thanks.
Go to the top of the page
+Quote Post

ayman_mastermind
Group Icon



post 13 Dec, 2008 - 02:27 AM
Post #3
hey thanks! i have always wondred how to do that in linux! now i know, thanks for your great post!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:07AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month