如何为 BSD grep 设置别名以使用输入递归搜索当前目录?
How do I alias BSD grep to search the current directory recursively with the input?
我和这里的作者有同样的问题:How do I write an alias for grep -R?
我想 gr whatever
执行 grep -nrI whatever .
。
我需要函数还是可以使用别名来实现?
我想不出一个优雅的方法来使用别名来做到这一点,但是如果你将 gr
定义为一个函数就很容易了:
$ gr() { grep -nrI .; }
如 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
grap() { if [ "$#" -lt 1 ]; then echo Please enter your search term.; else; grep -nrI "" .; fi; }
我和这里的作者有同样的问题:How do I write an alias for grep -R?
我想 gr whatever
执行 grep -nrI whatever .
。
我需要函数还是可以使用别名来实现?
我想不出一个优雅的方法来使用别名来做到这一点,但是如果你将 gr
定义为一个函数就很容易了:
$ gr() { grep -nrI .; }
如 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
grap() { if [ "$#" -lt 1 ]; then echo Please enter your search term.; else; grep -nrI "" .; fi; }