Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.