Linux – Grep default color option

command linegreplinux

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

Best Answer

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.

Related Question