Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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
share|improve this answer

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.

share|improve this answer
    
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 Dec 21 '12 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .