What’s Difference Between Grep, Egrep and Fgrep in Linux?

One of the renowned search tool on Unix-like systems which can be used to search for anything whether it be a file, or a line or multiple lines in file is grep utility. It is very vast in functionality which can be attributed to the large number of options it supports like: searching using string pattern, or reg-ex pattern or perl based reg-ex etc.

Difference Between grep, egrep and fgrep
Difference Between grep, egrep and fgrep in Linux

Due its varying functionalities, it has many variants including grep, egrep (Extended GREP), fgrep (Fixed GREP), pgrep (Process GREP), rgrep (Recursive GREP) etc. But these variants have minor differences to original grep which has made them popular and to be used by various Linux programmers for specific tasks.

Main thing that remains to be investigated is what are the differences between the three main variants i.e. ‘grep’, ‘egrep’ and ‘fgrep’ of grep that makes Linux users choose one or the other version as per requirement.

Some Special Meta-Characters of grep

  1. + – Equivalent to one or more occurrences of previous character.
  2. ? – This denotes almost 1 repetition of previous character. Like: a? Would match ‘a’ or ‘aa’.
  3. ( – Start of alternation expression.
  4. ) – End of alternation expression.
  5. | – Matching either of the expression separated by '|'. Like: “(a|b)cde” would match either ‘abcde’ or ‘bbcde’.
  6. { – This meta-character indicates start of range specifier. Like: “a{2}” matches “aa” in file i.e. a 2 times.
  7. } – This meta-character indicates end of range specifier.

Differences Between grep, egrep and fgrep

Some main differences between grep, egrep and fgrep can be highlighted as follows. For this set of examples we are assuming the file on which operation is being performed to be:

Linux grep Command
Linux grep Command

Grep Command

grep or Global Regular Expression Print is the main search program on Unix-like systems which can search for any type of string on any file or list of files or even output of any command.

Suggested Read: 12 Practical Examples of Linux grep Command

It uses Basic Regular Expressions apart from normal strings as a search pattern. In Basic Regular Expressions (BRE), meta-characters like: '{','}','(',')','|','+','?' loose their meaning and are treated as normal characters of string and need to be escaped if they are to be treated as special characters.

Suggested Read: 11 Advance ‘Grep’ Commands on Character Classes and Bracket Expressions

Also, grep uses Boyer-Moore algorithm for fast searching any string or regular expression.

$ grep -C 0 '(f|g)ile' check_file
$ grep -C 0 '\(f\|g\)ile' check_file
Linux grep Command Example
Linux grep Command Example

Like here, when the command is run without escaping '(' ')' and '|' then it searched for the complete string i.e. “(f|g)ile” in the file. But when the special characters were escaped, then instead of treating them as part of string, grep treated them as meta-characters and searched for words “file” or “gile” in the file.

Egrep Command

Egrep or grep -E is another version of grep or the Extended grep. This version of grep is efficient and fast when it comes to searching for a regular expression pattern as it treats meta-characters as is and doesn’t substitute them as strings like in grep, and hence you are freed from the burden of escaping them as in grep. It uses ERE or the Extended Regular Expression set.

In case of egrep, even if you do not escape the meta-characters, it would treat them as special characters and substitute them for their special meaning instead of treating them as part of string.

$ egrep -C 0 '(f|g)ile' check_file
$ egrep -C 0 '\(f\|g\)ile' check_file
Linux egrep Command Examples
Linux egrep Command Examples

Like here, egrep searched for “file” string when the meta-characters were not escaped as it would mean by the meaning of these characters. But, when these characters were escaped, then egrep treated them as part of string and searched for complete string “(f|g)ile” in the file.

fgrep Command

Fgrep or the Fixed grep or grep -F is yet another version of grep which is fast in searching when it comes to search for the entire string instead of regular expression as it doesn’t recognize the regular expressions, neither any meta-characters. For searching any direct string, this is the version of grep which should be selected.

Fgrep searches for complete string and doesn’t even recognize special characters as part of regular expression even if escaped or not escaped.

$ fgrep -C 0 '(f|g)ile' check_file
$ fgrep -C 0 '\(f\|g\)ile' check_file
Linux fgrep Command Examples
Linux fgrep Command Examples

Like, when meta-characters were not escaped, fgrep searched for the complete string “(f|g)ile” in the file, and when the meta-characters were escaped, then the fgrep command searched for “\(f\|g\)ile” all characters as is in the file.

We’ve already covered some practical examples of grep command you can read them here, if you want to get more out of grep command in Linux.

Learn 12 Practical Examples of Linux grep Command

Conclusion

Above highlighted are the differences between ‘grep’, ‘egrep’ and ‘fgrep’. Apart from difference in the set of regular expressions used, and speed of execution, rest command line parameters remain same for all the three versions of grep and even instead of “egrep” or “fgrep”, “grep -E” or “grep -F” are recommended to be used.

If you find any other differences between these three versions of grep, do mention them in your comments.

Gunjit Khera
Currently a Computer Science student and a geek when it comes to Operating System and its concepts. Have 1+ years of experience in Linux and currently doing a research on its internals along with developing applications for Linux on python and C.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 thoughts on “What’s Difference Between Grep, Egrep and Fgrep in Linux?”

  1. The following statement is misleading:

    Matching either of the expression separated by '|'. Like: “(a|b)cde” would match either ‘abcde’ or ‘bbcde’.

    In fact, both ‘abcde’ and ‘bbcde’ are matched with the ‘b’ of the ‘|’. The following statement would use both sides of the ‘|’ as intended:

    Matching either of the expression separated by ‘|’. Like: “(a|b)cde” would match either ‘acde’ or ‘bcde’.

    Reply
  2. really very good explanation.This is the first site and last site where i found perfect explanation of these 3 commands of linux

    Reply
  3. 1. pgrep is not Perl grep, it’s process grep (from the same package as pkill)
    2. “Perl” grep is pcregrep, grep that support Perl flavor of regular expressions
    3. You don’t need to use -C 0 as in your examples as 0 is the default value
    4. In the examples you could use either -o (display matched string) or –color (change color of matched string) so it’s more obvious why a line is displayed

    Reply
  4. I think that you got a typo in this sentence

    Like here, when the command is run without escaping ‘(‘ ‘)’ and ‘|’ then it searched for the complete string i.e. “(f|g)ile” in

    Should be

    Like here, when the command is run without escaping ‘\’ ‘\’ and ‘|’ then it searched for the complete string i.e. “(f|g)ile” in

    Reply
    • No, Actually i am throwing light towards those 3 meta-characters i.e. ( , ) and | . When these were not escaped, complete string was searched i.e. (f|g)ile ,but when they were escaped in the grep pattern, using backslash(\) in front of them, then their meaning changed.

      Reply

Leave a Reply to Ravi Saive Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.