0

I've got this alias:

alias gi='grep -r -i $1 ./*'

when i do

gi someString

It does the grep, but on some other string than that which I provide, usually with a "p/ or other such thing in it.

I'm using something similar for grepping history:

alias gh='history | grep $1'

Which works perfectly.

EDIT: I am on the /bin/bash shell, as per echo $SHELL.

Thanks!

Nathaniel
  • 457
  • 5
  • 14
  • You should probably share which shell you're using as well. – alroc Feb 06 '13 at 16:43
  • *alias* is just your particular shell built-in command. The *shell* you are using is probably *bash*. It doesn't actually matter, because *alias* is Posix and the answer applies to any Posix shell. – DigitalRoss Feb 06 '13 at 16:54
  • Sorry Digi, not sure what confusion you're addressing in this comment? Thanks though. – Nathaniel Feb 06 '13 at 16:59

3 Answers3

2

The alias mechanism merely substitutes for a word. Any other words on the same line are left in place, so typically one just replaces the command and leaves the arguments. This doesn't work well for your grep example because you want to rearrange the line.

Now, $1 will refer to the shell process (or shell function) parameters, in either case, not to words typed on the same line.

You would be better served in this case with a shell function, which should work on any Posix shell including bash.

gi () {
  grep -r -i "$1" ./*
}
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1

You cannot pass arguments to alias , please see this post for details. What you need is a function. Try something on the lines:

function gi() {
grep -r -i "$1" ./*
}

Hope this helps!

P.S.: As to why alias gh='history | grep $1' works is because it is the same as alias gh='history | grep. $1 is expanded when set an alias statically.

Community
  • 1
  • 1
another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
0

Try this alias:

alias gi='find . | xargs grep -i'

If you still just want to use alias to solve your problem, this will work.

petrsnd
  • 6,046
  • 5
  • 25
  • 34