In Bash, how to write alias or function for "export GREP_COLOR='1;32'; grep --color"?
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.
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'
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
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'
精彩评论