“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:

grep — search a file for a pattern
grep [ -v ] [ -b ] [ -c ] [ -n ] expression [ file ] …
Grep searches the input files (standard input default) for
lines matching the regular expression. Normally, each line
found is copied to the standard output. If the -v flag is
used, all lines but those matching are printed. If the -c
flag is used, only a count of matching lines is printed. If
the -n flag is used, each line is preceded its relative line
number in the file. If the -b flag is used, each line is
preceded by the block number on which it was found. This is
sometimes useful in locating disk block numbers by context.
In all cases the file name is shown if there is more than
one input file.
For a complete description of the regular expression, see ed
(I). Care should be taken when using the characters $ * [ ^
| ( ) and \ in the regular expression as they are also mean-
ingful to the Shell. It is generally necessary to enclose
the entire expression argument in quotes.
ed (I), sh (I)
Lines are limited to 256 characters; longer lines are trun-

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:

.th GREP I 3/3/73
.sh NAME
grep \*- search a file for a pattern

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.

From: Ken Thompson <ken@google.com>
Date: Sun, Jan 12, 2014 at 8:26 PM
Subject: Re: the birthday of grep
To: benjamin <benjaminrtz@gmail.com>

i dont remember what the date in .th macro means.
it probably is the last modified date of the man page,
not the command. i never recall that we ever noted
when a command was created. nromally, we
would only be able to say something like “it first
appeared in version X of the manual.
even after saying that, we were very careful not
to put junk into the utilities directory. grep was
a private command of mine for quite a while
before i made it public.
thats the long answer. the short answer is
“sometime before the 4th edition.”
kenOn Sat, Jan 11, 2014 at 1:14 PM, benjamin <benjaminrtz@gmail.com> wrote:Ken,Can you please confirm the birthday of grep?Grep was created by Ken Thompson as a standalone application adapted from the regular expression parser he had written for ed (which he also created).[3] In ed, the command g/re/p would print all lines matching a previously defined pattern.[4][5] Grep’s official creation date is given as March 3, 1973, in the Manual for Unix Version 4.[citation needed]

benjamin rualthanzauva

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.

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.

$ ed /etc/passwd
git:x:619:619:git daemon user:/:/bin/bash

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

$ grep bash /etc/passwd
git:x:619:619:git daemon user:/:/bin/bash

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.

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