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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn 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).
Mar 4, 2014 at 8:13
-r
, so it would just match from stdin
as usual.
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.
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.
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.
Mar 4, 2014 at 8:10