How to create grep alias with arguments

爷,独闯天下 提交于 2019-12-23 05:22:20

问题


Lets say i have this search term:

grep -i -r --include=*.xib "img_28301.png" ./

how would i go about creating an alias that allows me to do this:

xibsearch img_28301.png 

and do the same thing?


回答1:


It will be much cleaner to have a function for this:

xibsearch() {
    grep -i -r --include=*.xib "$1" ./
}

As alias doesn't support positional parameters.




回答2:


As @anubhava points out, functions are generally the way to go here.

However, in this case, if you really must use an alias, you can rearrange the grep a bit as follows:

alias xibsearch='grep -i -r --include=*.xib -f - ./ <<<'

The -f - tells grep to use expressions from a file, but since thew file here is -, then the expressions are read from stdin. Then we use a bash here-string to redirect the contents of a string to the stdin. This allows the grep expression to be placed right at the end of the command, which is what we need for an alias to work.

Thus, if you call:

xibsearch img_28301.png

the alias will resolve to the following command:

grep -i -r --include=*.xib -f - ./ <<< img_28301.png

I'd use with the function if I were you ;-).



来源:https://stackoverflow.com/questions/23680908/how-to-create-grep-alias-with-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!