Process Substitution
- 3 minutes read - 539 wordsProcess substitution is one of those things that is a very specific solution to a very specific problem. As you probably know, UNIX utilities are generally built so that they filter their input to their output. This lets you build pipelines very easily. Note that this is a UNIX family thing, and won’t work on other systems. For instance, if you want to do one or more transforms on a file, you can do this as a pipeline:
cat food | grep "tuna"In this case, the cat command reads the file food, and then passes
the contents of the file to its standard output, which is connected via
the pipeline to the input of the grep command. That filters its input
and throws away any lines that don’t match "tuna".
Perfectly standard UNIX stuff. But what if you have a utility that
doesn’t behave like this? For example, I use a Twine processor called
twee2 to turn Twine source into a playable HTML game.
Sidebar: the reason I’m doing this is because I want to put a build
number into the game file. I built a program that increments a build
number in a file each time you run it, and then I run that at the start
of the build process. I then use sed to substitute the build number
into the game file. This means that the build number the game file shows
automatically increments every time I build it. So, my pipeline should
look like this:
sed -e 's/VERSION/$$(cat buildinfo.txt)/g' amazing-game.tw2 | twee2 build`But, this doesn’t work, because twee2 doesn’t read standard input, probably
because it needs a command before the filespec, so it knows what to do. Instead,
you see an error. So, process substitution to the rescue. I can use the <( and
) operators to do the process substitution. If I do that, the shell runs the
process inside those delimiters, sends the result to a temporary file (for
example, /dev/fd/63), and then substitutes that filename into the command, and
cleans up the file when we’re all done. So, what does work is this:
twee2 build <( sed -e 's/VERSION/$$(cat buildinfo.txt)/g' amazing-game.tw2 ) amazing-game.html`Job done — each time I run it, the shell does all that magic, and I get
my HTML game file generated with auto-incrementing numbers. So,
triumphantly, I put the above in a Makefile. And it didn’t work.
But why not? There’s a tiny detail I slid past, that needs pointing out.
That detail is that the shell does this work. It’s not a feature of
every shell. Both bash and zsh do this, but plain old sh doesn’t.
And the make command uses sh by default. To use this technique, you
have to force the system to use the correct shell, so what works in a
Makefile is a line like this:
bash -c " twee2 build <( sed -e 's/VERSION/$$(cat buildinfo.txt)/g' amazing-game.tw2 ) amazing-game.html "Or, according to this StackOverflow
post,
you can set some versions of make to use bash or zsh instead. And there
you have it — that’s process substitution. More at
Wikipedia should you need
it, but that’s how I made it work for me. Comments? Thoughts?