Maintain text colors when wrapping command in bash script

2

So I've made a small script to search through my project for a word:

#!/bin/bash
grep -n $1 js/*/**.js
grep -n $1 js/*.js

When running these commands in the command line I will get some nice syntax highlighting. But when I run them in a script I loose the colors. How can avoid this?

worldsayshi

Posted 2012-12-20T12:32:26.627

Reputation: 123

Answers

3

This will do the trick, the --color flag takes care of coloured output. In your shell it is probably aliased.

#!/bin/bash
grep --color -n $1 js/*/**.js
grep --color -n $1 js/*.js

Bernhard

Posted 2012-12-20T12:32:26.627

Reputation: 1 017

0

Set these environment variables:

GREP_OPTIONS=--color=auto
GREP_COLOR=1;32

Where 1;32 is the color code for light green. You can change the color of highlighting using another code: http://hacktux.com/bash/colors.

Fábio Perez

Posted 2012-12-20T12:32:26.627

Reputation: 1 544

Hmm, I modified this and set export GREP_OPTIONS=--color in the beginning of the script; which works. This may be preferable to Bernhard's solution for larger scripts. – worldsayshi – 2012-12-21T09:18:46.913