我遇到与作者相同的问题:How do I write an alias for grep -R?
我希望gr whatever
执行grep -nrI whatever .
。
我是否需要一个功能,或者我可以使用别名吗?
答案 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; }