Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i usually have the following simple alias in my bashrc:

alias g="grep --color=always --exclude-dir=\*.svn\*"

but now i have to work on systems that only have GNU grep 2.5, hence no --exclude-dir argument.

now i need something like this to work:

alias g="grep --color=always $1 $2 | grep -v .svn"

but of course, the arguments get appended to the end of the alias. $1 and $2 are parsed when the alias is created not when it's called, even with strong quotes.

Can i solve that without resorting to extra scripts or functions?

share|improve this question
up vote 6 down vote accepted

No, you can't. As the bash manual states:

There is no mechanism for using arguments in the replacement text, as in `csh'. If arguments are needed, a shell function should be used (*note Shell Functions::).

Functions are the right thing for this, and can be as easy to write as an alias. In this case, it could be

function g () { grep --color=always "$1" "$2" | grep -v .svn ; }
share|improve this answer
2  
+1 Safer to quote the positional params. – glenn jackman Nov 2 '12 at 20:14
    
Very true. Edited to correct that. – Alan Shutko Nov 2 '12 at 20:32

You must log in to answer this question.

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