0

I added to following line to the "~/.bash_aliases" file:

alias svngrep='grep -R $1 ./* --exclude-dir ".svn"'

but it's not working.

It outputs (in my translation to English):

svngrep test
grep: test: File or directory not found

But with the full "grep" line it works just fine.

Any idea what's wrong with my alias?

2 Answers 2

1

$1 does not work in an alias. You need to define a function for that, for example in your .bashrc file.

function svngrep { grep -R --exclude-dir=".svn" "$@" . ; }

This should do the trick! However, make sure that your old alias is not around before trying it out from the command line:

unalias svngrep
function svngrep { grep -R --exclude-dir=".svn" "$@" . ; }
svngrep "teststring"

Quoting bash manual:

There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (...) For almost every purpose, aliases are superseded by shell functions.

0

You don't need a bash function (you may use it though, matter of taste), just remove the $1 ./* from your example and call it this way:

alias svngrep='grep -R --exclude-dir ".svn"'
svngrep phrase .

or here's an example I've been using for months:

alias rg='rgrep --exclude-dir=.svn --exclude=*.pyc'
rg phrase .

Of course, you can substitute . with any directory. I'd say using aliases is more intuitive, alias is an alias, function can do anything.

1
  • In principle yes, but there is no reason to prefer alias over function. Functions are not longer (instead of ="" you need to type { ;}), but are more flexible and can take arguments.
    – January
    Sep 17, 2012 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .