Explain

Explained! The Difference Between grep, egrep, and fgrep Commands

grep, egrep, fgrep. They all sound similar. So, what's the difference?

Christopher Murray
Christopher Murray

Table of Contents

I believe you are already familiar with the grep command. It may happen that you may come across terms like egrep and fgrep in some internet forum discussion and wonder what are these commands and whether they are related to grep.

In this tutorial, I'll discuss these grep variants explaining the difference and similarities.

What is egrep and fgrep? What is the difference between grep, egrep and fgrep?

The e in egrep stands for extended and the f in fgrep stands for fixed. egrep allows the use of extended regex and fgrep doesn't allow regex at all.

All this will make more sense soon when I explain them with examples.

You know that grep works on regex. But that's not the complete truth.

By default, the grep command works on basic regular expression. If you want more complex search, you need to use extended regular expression. See the difference between basic and extended regex.

The egrep command allows the use of extended regex.

The fgrep command on the other hand works on fixed string instead of a regex. This means that it takes the search pattern as it is for searching and thus it is faster than grep.

Although, you may still see egrep and fgrep in the wild, their status is deprecated. Many systems still include the binaries for these variants.

However, it is now the POSIX standard to use standard grep with special option flags (-E or -F). These flags help standard grep emulate the functionality of each respective command.

To summarize:

  • egrep and fgrep are deprecated commands and should be avoided.
  • egrep is equivalent to grep -E command and it allows the use of extended regex.
  • fgrep is equivalent to grep -F command and it uses a fixed string for search and hence it performs a faster search.

Understanding the difference between grep, egrep and fgrep with example

I have created a short text file to demonstrate the differences between these grep variants.

[email protected]:~$ cat test.txt 
this is a tasty test

Standard grep command

This is the standard version of grep designed for searching patterns using basic regular expressions. Using grep requires escaping meta-characters to generate the expected behavior.

Suppose I want to search for either tast or test. This is what I can use:

[email protected]:~$ grep 't\(a\|e\)st' test.txt

Notice how you have to escape the parenthesis? This is much easier to look at and understand with extended regex.

Extended Grep: Egrep or grep -E

Egrep was created to provide extended support for meta-characters. This means that it escapes these special characters by default. This saves time and generally makes the expressions easier to read.

The command I used above can be re-written as the much more legible:

[email protected]:~$ egrep 't(a|e)st' test.txt

The egrep binary does not consistently support the { character. This can result in unexpected behaviors.

Some versions of egrep allow them to be interpreted properly as literal characters if escaped (\{). However, the best practice for matching a literal { is to enclose in square brackets [{].

grep vs egrep

Fixed Grep: Fgrep or grep -F

Software professionals know that even as computational capability has increased profoundly, optimization can never be ignored. Grep is a very powerful tool, but it can sometimes use system resources unnecessarily.

This is where the idea of fixed grep came from. This software is ideal for use cases where you need to match an exact string.

It does not feature the powerful expression tools of the other grep variants. That weakness is actually it's super power. This makes the tool much faster.

10 Practical Grep Command Examples for Developers
The grep command is used to find patterns in files. This tutorial shows some of the most common grep command examples that would be specifically beneficial for software developers.

As I mentioned earlier, you should avoid using egrep and fgrep because they are deprecated commands. Using grep -E and grep -F is the way forward.

I hope you now have a better understanding of grep, egrep and fgrep. If you have something to add, please feel free to drop a comment.



Join the conversation.