Nathaniel
Nathaniel

Reputation: 457

Linux Alias, what's wrong with it?

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!

Upvotes: 0

Views: 157

Answers (3)

petrsnd
petrsnd

Reputation: 6136

Try this alias:

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

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

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146123

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" ./*
}

Upvotes: 2

another.anon.coward
another.anon.coward

Reputation: 11395

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.

Upvotes: 1

Related Questions