“grep was a private command of mine for quite a while before i made it public.” -Ken Thompson

The history of grep, the 40 years old Unix command

grep is a Unix command line utility (well most Unix utilities are command line) that searches the input files for pattern and prints lines that contain the pattern. If you are reading this you, you are probably no stranger to grep.

grep was written by Ken Thompson, the same guy who wrote Unix. grep first appeared in Unix v4 with limited features as compared to today’s grep. This is what the man page for grep looks like in Unix v6:

The wrong birthday

Perhaps because the grep man page in Unix v4 has the dates 3/3/1973, a lot of articles on the Internet assume this is the creation date of grep, including grep article on Wikipedia until I edited it.

Unix v4 grep man page:

Due to the lack of attribution to the creation date in Wikipedia and because I could not find Unix v4 man pages on Google, I turned to Reddit and emailed the man Ken Thompson himself with little hope for a reply. On Reddit, someone promptly replied with a link to the Unix v4 man page. And the next day I got this in my inbox.

Overjoyed I ran to my wife and showed her the mail though there is little chance she will ever understand the excitement. Thank you Ken and /r/linux.

The invention of grep and what it means

One guy McIlroy claimed grep was invented for him. On a lighter note, I suppose. Or not.

“One afternoon I asked Ken Thompson if he could lift the regular expression recognizer out of the editor and make a one-pass program to do it. He said yes. The next morning I found a note in my mail announcing a program named grep. It worked like a charm. When asked what that funny name meant, Ken said it was obvious. It stood for the editor command that it simulated, g/re/p (global regular expression print).”

Chapter 9, On the Early History and Impact of Unix Tools to Build the Tools for a New Millenium

The origin of the name grep is also confirmed by Dennis MacAlistair Ritchie (September 9, 1941 — c. October 12, 2011), the guy who wrote C. If you remember, Dennis passed away a week after Steve Jobs and sadly the world hardly knew it.

[It has been alleged that the source is from the title of a paper “A General Regular Expression Parser”, but dmr confirms the g/re/p etymology —ESR]

The Jargon File

I never use ed, which is a line editor also written by Ken Thompson, except to demonstrate this functionality.

The input file is /etc/passwd and g/bash/p does the search. With grep, we would do:

fgrep, egrep

fgrep and egrep originally existed as separate executable because they were another command written by different author. Historically, grep and egrep took turns being faster than the other. They have been almalgamated into one command. Today fgrep and egrep existed solely for compatibility to enable the use of ancient scripts that used direct invocation of fgrep and egrep. The proper way to use them today is grep -F and grep -E. The -E and -F options are specified by POSIX. POSIX defines standards for software compatibility between Unix and other operating systems.

Al Aho also a researcher at Bell Labs and the co-author of AWK wrote egrep and fgrep one weekend in 1975.

In the beginning, Ken Thompson wrote the searching tool grep. It selected and printed lines from its file arguments that matched a given regular expression. In 1975, just after the release of Sixth Edition UNIX, Al Aho decided to put theory into practice, and implemented full regular expressions (including alternation and grouping which were missing from grep)and wrote egrep over a weekend. Fgrep, specialised for the case of multiple (alternate) literal strings, was written in the same weekend. Egrep was about twice as fast as grep for simplecharacter searches but was slower for complex search patterns (due to the high cost of build-ing the state machine that recognised the patterns).

Ever since, each of the tools has sporadically improved its performance, mostly as acfriendly rivalry between the owners of grep (Thompson, and later on, McMahon) and egrep(Aho).

Grep Wars: The Strategic Search Initiative, Andrew Hume, 1987

egrep and fgrep appeared in the grep man page on Unix v8.

GNU grep

If you are on Linux, you are using the GNU grep. Unless of course you have deliberately installed some other grep. On the ESXi console, you have a limited busybox version of grep. On Mac, it’s BSD grep. To find out which grep is installed run grep -V. Elsewhere the grep installed by default is most likely the platform’s own version of grep. I suppose you could compile GNU grep if you so wish but we will not dive into that here.

GNU grep was originally written by Mike Haerkal. Version 1 was announced in 1988. However, grep is mentioned in the second issue of GNU’s Bulletin from Jan 1987 “The GNU `ls’, `grep’, `make’ and `ld’ are in regular use”.

I asked Mike and RMS but both don’t remember the details. Mike said

Summer of 1988. Don’t remember the exact date offhand, but probably
sometime in late June or early July of that summer.

and RMS replied

I don’t remember that sort of detail after 27 years.
If we said it in the bulletin, it must be true.

It is possible GNU grep existed in some very basic form before Mike got his hands on it. It is interesting to note Mike Haerkal is also the co-architect of AMD-V, the virtualization extension for x86 CPU’s from AMD.

If you have interesting bits of grep’s history to share, I would be more than happy to hear and add to this post.

Thank you for reading.

--

--

I can handle boredom

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store