Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write an alias for grep -R?

Tags:

grep

bash

I end up typing

grep -Rni pattern .

and awful lot. How do I make this into an alias like

alias gr='grep -Rni $@ .'

Running that gives:

$ gr pattern
grep: pattern: No such file or directory

Even though the alias looks fine:

$ type gr
gr is aliased to `grep -R $@ .'

It seems that the $@ and the . get swapped when it's actually executed.

like image 450
numerodix Avatar asked Apr 12 '10 09:04

numerodix


People also ask

How do I get an alias name in Linux?

To see a list of aliases set up on your linux box, just type alias at the prompt. You can see there are a few already set up on a default Redhat 9 installation. To remove an alias, use the unalias command.


2 Answers

Try this:

$ alias gr='grep -Rnif /dev/stdin . <<<'
$ gr pattern
./path/file:42:    here is the pattern you were looking for

This also works:

$ alias gr='grep -Rnif - . <<<'
like image 154
Dennis Williamson Avatar answered Nov 02 '22 00:11

Dennis Williamson


make a function instead of alias. Save it in a file eg mylibrary.sh and whenever you want to use the function, source the file

eg mylibrary.sh

myfunction(){
 grep -Rni ...
}

#!/bin/bash
source mylibrary.sh
myfunction 
like image 40
ghostdog74 Avatar answered Nov 01 '22 23:11

ghostdog74