Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

$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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 '12 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.