Working with the m4 Macro Language | Eur Ing Dr James P. Howard II Working with the m4 Macro Language | Eur Ing Dr James P. Howard II

Dr James P. Howard, II
A Mathematician, a Different Kind of Mathematician, and a Statistician

image representing a theme in this article

Working with the m4 Macro Language

I recently ran into a problem where I needed to generate a lot of configuration files with slightly different parameters included. This was all being fed into a simulation so we could test different values of each parameter in the simulation. The details are not important. I needed a way to generate 297 configuration files.

I ended up settling on the m4 macro language to generate these files. It was about three lines of m4 in a base file and looping over it in a shell script with the parameters occupying environmental variables. The details here are not what’s important. What’s important is m4.

The m4 macro language dates all the way back to 1977 first appearing in Seventh Edition Unix. The language is obscure, even by Unix standards. There is no m4 programming webpage. There is no O’Reilly book. Most people who have any familiarity with it know it because the Sendmail system, which has used m4 to manage its configuration files for decades. It also underlies Autoconf, and your usage there is kind of indirect. I cannot think of any other mainstream use of m4. So I went on a bit of a hunt.

I found a pair of videos on YouTube introducing m4. The first looks to be a talk given to a meetup in Portland:


And the second is from the Australian GovHack conference, showing a variety of neat ways m4 was being used in the Australian government:


If you can find a copy of it, the m4 documentation is only like five pages. There’s a copy in this PDF, starting on page 85.

The last few resources of merit I found are:

After this, sources start to get thin on the ground. As rare as a good use of m4 is, it was absolutely the best choice for what I was doing. It’s always worth remembering what’s already on your computer since you never know when the right tool is already there.