Customized text searches by using grep

I tried to ease text searches so made a customized grep:

g () {
if [ -n "$2" ]
then
 i=
 for s in $2
 do
	i="$i --include=*.$s"
 done
 else
	i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg'
fi
grep -P -e \'$1\' -r "$i"
}

but I used it then

$ g sys ini
grep:  --include=*.ini: No such file or directory

if I insert 'echo' to last line for tracking and test:

echo grep -P -e \'$1\' -r "$i"

then reload the alias file and get to shell,

$ g sys ini
grep -P -e 'sys' -r  --include=*.ini

it'll echo correctly to become perfectly a grep command line...

Any sincere guide to help me out by which we all have benefits ?

What's your OS and grep version?

Howsoever, try again with the double quotes around $i removed.

What output do you get if you add a set -x statement before the first if? It should show you what it's doing and why.

Does this help? Paste the output back in here if it is confusing and we can work through it.

Kind regards,
Robin

I doubt this will work as intended the --include= option is supposed to filter the filenames passed to grep. However, your g() function doesn't pass any filenames on the grep command.

As RudiC pointed out, the double quotes are causing the --include= option to be treated as a filename for grep.