Bash alias arguments?

Ovid on 2006-01-17T00:29:41

I find myself writing this all the time in the bash shell:

grep -r $some_string .|grep -vE 'svn|blib'

It seems that either my bash shell knowledge is too limited or bash aliases cannot take argument (for $some_string). How do you handle that? A bit of Perl or a simple shell script? (I believe zsh allows aliases to take arguments but I really don't want to learn another shell).


functions

itub on 2006-01-17T00:36:08

You can also define functions in your .bashrc. Functions are more flexible than aliases, so you'll probably be able to do what you want.

Re:functions

Shlomi Fish on 2006-01-17T19:25:16

A function that does that is:


rgrep() { grep -r "$1" . | grep -vE 'svn|blib' ; }


And then you say:

$ rgrep $string

Advanced Bash-Scripting Guide

rblackwe on 2006-01-17T01:03:16

The Advanced Bash-Scripting Guide might profide you with some help.

http://www.tldp.org/LDP/abs/html/

Look at ack

petdance on 2006-01-17T01:46:44

Look at my program ack, available as a distro in my CPAN directory.

It doesn't exclude blib yet, but it does ignore .svn and CVS directories, and it does recursing into directories by default.

Svk

chromatic on 2006-01-17T02:21:33

That's why I use Svk. Well, okay... I use it for other reasons, but that's a nice benefit.

(No, bash aliases can't take arguments.)

rgrep for excluding .svn and blib

markjugg on 2006-01-17T03:21:43

I suggest rgrep by Michael Schwern.

This seems like one of those things that seemed "too small to put on CPAN", but probably isn't.

Alias syntax

ChrisDolan on 2006-01-17T05:34:37

In csh, the alias syntax would be:

    alias stringgrep 'grep -r \!* .|grep -vE "svn|blib"'

I suspect the bash syntax is similar.

Re:Alias syntax

Smylers on 2006-01-17T11:10:36

No, there isn't an equivalent syntax for Bash aliases. This is because in C Shell the ! there is a history substitution, and history works differently in Bash from in C Shell.

As others have said, use a function; there isn't really any advantage in using a Bash alias over a function (other than you're more likely already to know the alias syntax).

I think this should do what you want (untested):

function kerplop
{
  grep -r "$@" . | grep -vE 'svn|blib'
}

(Function name courtesy of meta.)

Smylers

Bash functions

robin on 2006-01-18T22:16:20

Bash functions are amazingly useful, and ridiculously simple. Just put something like this in your .bashrc.

ovidgrep () {
  grep -r "$1" .| grep -vE 'svn|blib'
}