Since egrep is essentially grep -E I would expect it to be an alias that calls grep with that option. However on my system

file `which egrep`

shows it is an executable. Also egrep as an alias only invokes egrep with the color option. Why is it not an alias? Do they compile a separate binary for egrep?

Just curious...

link|improve this question

68% accept rate
feedback

2 Answers

They used to be separate executables, but these days it's usually a single executable with three hard links ("grep", "fgrep" & "egrep") all pointing to the same program: the program checks which name it was started as, and behaves accordingly.

So what you're running is an enhanced egrep with the features of the other two programs included: and calling it by different names, or with -E gives you the features you expect.

If you type

ls -i `which grep`

and the same for fgrep and egrep you may see that they all have the same inode number, which means they are all the same file.

link|improve this answer
Well at least on my system they have different inode number (off by 3). but in principle I understand your point. they could be hard symlinks and the grep program could look up argv[0] to find out what options to apply. – iamrohitbanga Nov 19 '11 at 14:13
feedback

It depends on the OS and the version of the grep tools installed.

Some examples for systems I happen to have access to:

  • Ubuntu 11.04, GNU grep 2.6.3: /bin/grep, /bin/fgrep, and /bin/egrep are three distinct executable files with different sizes.

  • GNU grep 2.10 (just released a few days ago), built from source: the same.

  • Cygwin: the same.

  • CentOS 5.6 (a clone of Red Hat), GNU grep 2.5.1: /bin/egrep and /bin/fgrep are symlinks to /bin/grep.

  • GNU grep 2.5.1 built from source: egrep and fgrep are small shell scripts that invoke grep.

  • Solaris 9: three different files (Solaris tools, not GNU).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.