Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-22-2010, 02:44 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
Changing color of output from bash script

I am writing a bash script that utilizes the output of another script (which I will refer to as script#2.) Script#2 is not owned by me, I cannot modify it. All of the output from script#2 is blue, which makes it difficult for me to read.

I would like to have the output of it changed to grey. Is there a way I can do that in my script? A command I can pipe the output to?

Edit: One other question related to this. I put a trap function in my script that works well. Script#2 essentially runs a tail -f. When I ctrl+c to stop it, it stops script#2 and never calls the trap in my script. Is there any way I can work around that?


Last edited by Fliggerty; 04-22-2010 at 03:06 AM.
Old 04-22-2010, 07:44 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Chances are the colors come from embedded ansi escape sequences. If so, you could fairly easily use sed to change or remove them. Try piping the script through cat -v to see what non-printing characters exist in the output. For example, if I run ls through it with colors on, I get:

$ ls --color|cat -v

"^[[01;34m", for example, is the escape sequence for bright blue. The ^[ at the beginning is a graphical representation of a literal ascii escape character, (in the script code it will probably be represented as \e). The numbers between the second [ and the m are the modes that set the display colors. Multiple modes are strung together with semicolons. This page lists the possible codes. You'll want the "set graphics mode" section.

So to change the blue to red, for example, simply change the number with sed.
ls --color|sed 's/;34/;31/g'
To remove all the color codes, you can use an expression like this (I found this on the net, so it may not remove every code possible, but it should work for most of the common ones). Note that the escape has to be matched using the hex value.
sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"
Hopefully this will help you.

Regarding your second question, I think we'd need to see the actual code to figure out what's going on.
1 members found this post helpful.
Old 04-22-2010, 12:25 PM   #3
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Everything you want to know and more:
1 members found this post helpful.
Old 04-24-2010, 08:40 AM   #4
Senior Member
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
+1 for arch wiki.
0 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Bash Color Prompt carlosinfl Red Hat 2 03-25-2010 01:16 AM
I want my script output in bold and color fonts gdan2000 Linux - General 6 03-16-2009 06:29 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
[bash] changing the color set MD3 Linux - Software 2 04-27-2005 12:38 PM
Changing the color of ls output? exitsfunnel Linux - Newbie 1 02-19-2004 11:20 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration