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

grep is one of the most commonly used command in linux. It feels to me that its basic feature to highlight the string you searched for on the output lines. this can be achieved by --color option.

Typing --color every single time is annoying and also not productive. Is there any way to change grep to behave as grep --color.

I tried writing a small script named it grepd and added this to my PATH variable. But the script doesnt work on the input grepd . Any suggestions please.

grep --color $1 $2
share|improve this question
up vote 6 down vote accepted

Simply add the following alias to your shell's configuration file, e.g. .bashrc or .bash_profile (depending on which you use, see here):

alias grep='grep --color=auto'

You can simply use it as grep.

There's usually no need to make scripts when simple command aliases do the same thing just fine. In fact your script wouldn't even work if you wanted to pass more options to grep. In case you need a tiny snippet that can deal with arguments, you should use functions.

share|improve this answer
exec grep --color "$@"

This illustrates the standard way of "wrapping" a command with a shell script, when the command doesn't quite work the way that you like.

The exec avoids creating an extra process (one for the script and one for grep). You could leave it out if you like.

The "$@" is replaced by all of the script's arguments, no matter how many there are. It correctly preserves arguments with spaces and other characters that are special to the shell.

share|improve this answer
The "exec grep" should be "exec /bin/grep" – Berend de Boer Jul 23 '15 at 6:50
@BerenddeBoer Not really. "exec grep" would work just fine. There's nothing wrong with searching the path for grep. – Kenster Jul 24 '15 at 1:47

You must log in to answer this question.

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