Linux: Changing STDOUT text color?

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
I was able to find a site that had some help with changing the text color for your PROMPT (http://www.linuxlookup.com/html/articles/custom-prompts.html)

But is there a way to do this with the echo command?

I've got a Bash Script that spits out a bunch of columns and rows of info, it would be a GREAT help if I could make every other row a different text color.

Any help is much appreciated!

thanks.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
$PROMPT is just a string, and it is printed to the terminal in the same manner that any other text is. Those same color codes will work for anything.

Hm, it seems whoever wrote that page kind of goofed the syntax though.

echo -e '\033[34mBLUE!\033[0m'

or

printf '\033[34mBLUE!\033[0m\n'

(most people use echo -e, i prefer printf myself)
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Here is a table I made for my .bashrc file so that I could change colors easy in my prompt.


#consule colors!
RED="\[\033[0;31m\]"
LRED="\[\033[1;31m\]"
GRN="\[\033[0;32m\]"
LGRN="\[\033[1;32m\]"
BLU="\[\033[0;34m\]"
LBLU="\[\033[1;34m\]"
PUR="\[\033[0;35m\]"
LPUR="\[\033[1;35m\]"
CYA="\[\033[0;36m\]"
LCYA="\[\033[1;36m\]"
YEL="\[\033[0;33m\]"
LYEL="\[\033[1;33m\]"
BRN="\[\033[0;33m\]"
GRY="\[\033[1;30m\]"
LGRY="\[\033[0;37m\]"
WHT="\[\033[1;37m\]"
NON="\[\033[0m\]"

# backgroundcolors
BGRY="\[\033[40m\]"
BRED="\[\033[41m\]"
BGRN="\[\033[42m\]"
BYEL="\[\033[43m\]"
BBLU="\[\033[44m\]"
BPUR="\[\033[45m\]"
BCYA="\[\033[46m\]"
BWHT="\[\033[47m\]"

Then for my bash prompt I use this:

PS1="\n-{$RED\u$LGRN@$GRN\h$NON}-$PUR \t$GRN\n$BRED\w >:$NON "
export PS1

I think I got the info from that same page as the one your linked to. I just put it in a format that is actually usefull.

You could probably just copy and paste that into a script that you could use to format outputs a bit easier.


edit:

I suppose you would have to change them from
RED="\[\033[0;31m\]"
to something like
RED="\033[0;31m"

to get it to work so that you can do something like
echo -e "$RED hellodog $NON"
 

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
Thanks for the replies guys!

The echo -e with the parameters did the trick great!