18

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.

#!/bin/bash
grep --color $1 $2
1
  • for what it's worth, while the alias option in the accepted answer is probably really the way to go, I'm guessing that the problem you had here was primarily one related to the way you used positional parameters ($1 and $2). A better option for the second line of this script would have been: grep --color "$@" -- $@, when in double-quotes (") specifically, will expand to the list of arguments the script was passed with, and in the same manner... so for example, if a * is in your grep pattern (e.g. .*), that won't expand, and would be passed on to grep... among other things.
    – lindes
    Nov 4, 2022 at 21:57

3 Answers 3

28

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.

5
  • 12
    Or equivalently, set the GREP_OPTIONS environment variable: export GREP_OPTIONS='--color=always'
    – Jérôme
    Mar 28, 2017 at 21:41
  • This doesn't add the color option if you use fgrep or egrep, you would need seperate aliases for those. Also, @Jérôme: GREP_OPTIONS is marked as deprecated in the grep man page.
    – Thayne
    Jan 23, 2018 at 5:23
  • @Thayne only for GNU grep and not others like FreeBSD grep. And as far as I can tell, it's been 4 years since that was changed and it's still supported. Depreciated does not mean removed.
    – cde
    Feb 2, 2018 at 20:14
  • 1
    This is what I usually do, however it doesn't work right in some cases. For example: some_command | xargs grep foo will not be colorized because xargs doesn't use the alias.
    – TM.
    Apr 30, 2018 at 20:17
  • 1
    From info grep: "The ‘GREP_OPTIONS’ environment variable of ‘grep’ 2.20 and earlier is no longer supported, as it caused problems when writing portable scripts."
    – l0b0
    Jul 13, 2023 at 3:34
3
#!/bin/sh
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.

2
  • The "exec grep" should be "exec /bin/grep" Jul 23, 2015 at 6:50
  • 3
    @BerenddeBoer Not really. "exec grep" would work just fine. There's nothing wrong with searching the path for grep.
    – Kenster
    Jul 24, 2015 at 1:47
-2

try putting export GREP_COLORS='AUTO' in your ~/.bashrc - for me it works.

from man grep

       --color[=WHEN], --colour[=WHEN]
          Surround  the  matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences
          to display them in color on the terminal.  The colors are defined by the environment variable GREP_COLORS.  The deprecated environment variable GREP_COLOR is still supported, but  its  setting
          does not have priority.  WHEN is never, always, or auto.
1
  • 8
    GREP_COLORS is the set the actual colours. Setting to AUTO should not show any colours. export GREP_OPTIONS='--color=auto' should be the answer.
    – user137369
    Jan 19, 2018 at 13:23

You must log in to answer this question.

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