tags:

views:

60

answers:

3

grep doesn't allow setting color by

grep --color='1;32'

(1 meaning bold, and 32 meaning green). It has to use GREP_COLOR by

export GREP_COLOR='1;32'

and then use grep --color

How do we alias or write a function for grep so that we have 2 versions of grep (say, grep and grepstrong), one for usual green font, and the other one, green font with a black (or white) background?

alias grep='export GREP_COLOR="1;32"; grep --color'

won't work because if we use

grep some_function_name | grep 3

then the above alias will generate results of the grep, and pipe into export, so the second grep won't get any input at all and just waiting there.

A: 

Are you putting this in your .bashrc file? Just do it like this:

export GREP_COLOR="1;32"
alias grep='grep --color'

and you should be good to go

Cfreak
for 2 versions of grep... is the problem
動靜能量
+4  A: 

With bash, you can set environment variables for just a single command by prepending the command with "key=value" pairs:

GREP_COLOR='1;32' grep --color <whatever>

Example:

echo foo | VAR=value bash -c 'read line; echo $VAR: $line'

So in your case, just say:

alias grep='GREP_COLOR="1;32" grep --color'
Sean
note -- without the semicolon
動靜能量
also note: need to escape the grep for grepstrong -- otherwise invoking grep will also invoke that color as well. See the lines in my answer on this page.
動靜能量
A: 

Using Sean's answer, the alias for grepstrong needs to escape grep, so that it won't invoke the grep using the green font color.

alias grep='GREP_COLOR="1;32" grep --color'
alias grepstrong='GREP_COLOR="1;34;46" \grep --color'
動靜能量