Command line 从grep中删除行号选项

Command line 从grep中删除行号选项,command-line,grep,command-line-arguments,alias,Command Line,Grep,Command Line Arguments,Alias,我使用grep的别名,将行号添加到输出中: alias grep="grep -n -I --color" 对于某个命令,我想从grep命令中删除-n。我找不到“请勿添加行号”标志。这样的功能可用吗?或者我必须添加一个不带行号的额外别名吗?您可以使用命令删除所有参数 command grep -I --color 只需使用反斜杠,因为它将恢复为默认的grep命令,而不是您的别名: \grep -I --color 我意识到这是一个老问题,但我遇到了同样的问题。此解决方案可行,但不是最优雅的

我使用grep的别名,将行号添加到输出中:

alias grep="grep -n -I --color"

对于某个命令,我想从grep命令中删除
-n
。我找不到“请勿添加行号”标志。这样的功能可用吗?或者我必须添加一个不带行号的额外别名吗?

您可以使用
命令
删除所有参数

command grep -I --color

只需使用反斜杠,因为它将恢复为默认的
grep
命令,而不是您的别名:

\grep -I --color

我意识到这是一个老问题,但我遇到了同样的问题。此解决方案可行,但不是最优雅的:

# Not part of your original question, but I learned that if an alias
# contains a trailing space, it will use alias expansion. For the case
# of 'xargs', it allows you to use the below grep aliases, such as the
# 'chgrep' alias defined below:
#   grep -l 'something' file1 file2 ... | xargs chgrep 'something else'
alias xargs='xargs '

export GREP_OPT='-n'
# Note the $(...) INSIDE the 'single quotes'
alias grep_='\grep --color=auto -I $(echo "$GREP_OPT")'
# Due to alias expansion, the below aliases benefit from the 'GREP_OPT' value:
alias cgrep='grep_ -r --include=\*.{c,cc,cpp}'
alias hgrep='grep_ -r --include=\*.{h,hh,hpp}'
alias chgrep=cgrep_ --include=\*.{h,hh,hpp}' # Not sure if there's a way to combine cgrep+hgrep
...
alias pygrep='grep_ -r --include=\*.py'
在需要非编号输出的情况下,取消设置
GREP_OPT
(或从中删除
-n
,就像使用它包含其他标志一样)

在上面的示例中,我使用了
grep\uu
而不是
grep
,只是为了说明别名将根据需要链接/使用别名扩展,一直返回到原始命令。您可以随意给他们取名为grep
grep
。您将注意到,原始的
grep\uu
使用命令
\grep
,因此别名扩展在该点停止


如果有更好的方法,我完全愿意接受建议(因为设置/取消设置
GREP_OPT
并不总是最优雅的)。

那么,threre的no
--no line number
或类似选项?没有
-n
标志的GREP就可以做到这一点,问题是如何禁用默认值(别名),最简单的方法是\还原到原始(不带别名)命令。哦,这太棒了-我是说
反斜杠
:不知道有避免别名的方法!是的,非常好。有时最简单的提示是最好的。我知道
GREP_OPTIONS
是存在的,但是GREP的主页声明:
这个变量指定了要放在任何显式选项前面的默认选项。由于这会在编写可移植脚本时产生问题,因此在将来的grep版本中将删除此功能,如果使用,grep将发出警告。请改用别名或脚本。