Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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 ;-).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top