如何使用BSD grep别名来递归搜索当前目录?

时间:2018-04-02 16:17:43

标签: bash shell grep zsh bsd

我遇到与作者相同的问题:How do I write an alias for grep -R?

我希望gr whatever执行grep -nrI whatever .

我是否需要一个功能,或者我可以使用别名吗?

3 个答案:

答案 0 :(得分:3)

GNU grep manpage中所述,如果没有目录传递给-r,它将在当前目录下工作:

-r, --recursive
  Read all files under each directory, recursively, following symbolic
  links only if they are on the command line.  Note that if no file
  operand is given, grep searches the working directory.  This is
  equivalent to the -d recurse option.     

所以在这种情况下,alias就足够了:

alias gr='grep -nrI'           

您现在可以正常使用gr

$ gr string

答案 1 :(得分:2)

我想不出用别名来做这件事的优雅方法,但如果你将gr定义为一个函数,那就很容易了:

$ gr() { grep -nrI $1 .; }

答案 2 :(得分:0)

grap() { if [ "$#" -lt 1 ]; then echo Please enter your search term.; else; grep -nrI "$1" .; fi; }