In my coding, I always do grep -rnIi "abcabc" . --color
But, how to make an alias that can let me type only one word with my search string? Is that possible?
In my coding, I always do grep -rnIi "abcabc" . --color
But, how to make an alias that can let me type only one word with my search string? Is that possible?
Pay attention to the tags on this question. This answer is the correct one given the version of grep
that's included with CentOS 6 is below version 2.11
I don't believe you can do this using just an alias (at least not in the form that you've written it - see @Graeme's answer for an alternative syntax).
NOTE: I'll also mention here that @Graeme's solution though correct does not work on CentOS 5 & 6. I've confirmed this and it does in fact appear to be an issue with the version of grep
(2.11 or older) that's bundled with those distros. The issue appears to be with grep
not, by default, searching STDIN when no path argument is provided with the -r
switch. Running grep -rnIi --color "string"
just hangs indefinitely, attempting to search STDIN.
Aliases cannot take arguments being passed into them. They're more synonymous to a macro where they can get expanded by the shell to something else.
$ alias ll='ls -l'
This can then be used like so:
$ ll
-or-
$ ll somedir
But somedir
isn't passed to ll
, rather ll
is expanded in place to ls -l
and then replaces ll
in your command.
You can use a function in the shell instead. Functions can take arguments.
$ function mygrep { grep -rnIi "$1" . --color; }
You then use it like this:
$ mygrep "blah"
Running grep -rnIi --color "string" just hangs indefinitely, waiting for a file/path argument to search
) was in fact not waiting for a file/path, but was already grepping the standard input. Graeme's mygrep 'abcabc' .
should work fine on CentOS as on any other "normal" unixes (using bash/sh/many others).
– Olivier Dulac
Mar 04 '14 at 08:13
-r
, so it would just match from stdin
as usual.
– Graeme
Mar 04 '14 at 11:07
grep
on CentOS due to the dated version of grep that's included. Yes, I'm aware that the grep '...' .
will work, it's just that you cannot do that as an alias, so the function method is the correct solution given the tags set on this particular Q.
– slm
Mar 04 '14 at 12:35
alias mygrep='grep -rnIi --color'
: when you then mygrep "something" somewhere
it will expand to grep -rnIi --color "something" somewhere
(and therefore will work as expected). you can have as many arguments after "mygrep" as you want: the first one will be the regexp to look for, the next one(s) will be places to look at/in. (as the alias only expands to "grep -options", the rest of the line will be appended to it by the shell)
– Olivier Dulac
Mar 04 '14 at 14:47
grep
+ -r
it wasn't feasible to do it, hence my A.
– slm
Mar 04 '14 at 15:47
This can actually be an alias. The --color
doesn't have to be at the end, and grep
always searches the current directory with -r
if no directory option is given (at least GNU grep
does). Therefore you can do:
alias mygrep='grep -rnIi --color'
The behaviour of searching the current directory by default was introduced in GNU grep 2.11
. From the changelog:
If no file operand is given, and a command-line -r or equivalent option is given, grep now searches the working directory. Formerly grep ignored the -r and searched standard input nonrecursively. An -r found in GREP_OPTIONS does not have this new effect.
So, on versions >=2.11
of GNU grep
, usage of the alias would be as follows:
mygrep PATTERN [PATH...]
For versions of grep
before this, you would have to specify the path every time @slm's answer using a function is the best way. Although you could achieve the same usage as above by writing the function like this:
mygrep () { grep -rnIi --color "$1" "${2:-.}"; }
Assuming, of course, that there is no requirement for the --color
to be at the end as in the Q...
grep
, I just tried it on Fedora 19 and it worked fine. I've confirmed that it def. doesn't work on CentOS 5 or 6. CentOS 6 = 2.6.3, Fedora 19 = 2.14.
– slm
Mar 04 '14 at 02:41
mygrep something somewhere
(ex: mygrep "abcabc" .
, for the OP's example). Grep needs both something to search for, and where to search it (a file, or a directory, both are valid here). The alias only provides the options, as those can (and should) be placed before the arguments.
– Olivier Dulac
Mar 04 '14 at 08:10
grep (GNU grep) 2.16
– Graeme
Mar 04 '14 at 10:16
grep
in searching the current directory with the -r
switch, this solution won't work on CentOS 6 + it's def. version of grep
.
– slm
Mar 04 '14 at 12:43