问题描述
的grep
不允许通过
grep --color='1;32'
(1含义大胆和32含义绿色)。它由
(1 meaning bold, and 32 meaning green). It has to use GREP_COLOR by
export GREP_COLOR='1;32'
然后用的grep --color
如何做我们的别名或写grep的一个功能,所以我们有2个版本的grep(比如grep和grepstrong),一个通常的绿色字体,和其他一,绿色字体带黑色(或白色)的背景?
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
那么上面的别名将产生的grep的结果,以及管进入导出
,所以第二个grep的根本不会得到任何输入,只是等候在那里。
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.
推荐答案
使用bash中,你可以设置环境变量由$ P $只是一个单一的命令ppending以键=值对的命令:
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>
例如:
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'
这篇关于在bash中,如何写别名或功能&QUOT;出口GREP_COLOR ='1; 32'; grep的--color&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!