A few weeks ago, I received my first pull request on the zsh-dwim GitHub repository. Much to my surprise, it was a change to make zsh-dwim work on Mac OS X. I made a mistake that I’ve made a few times in the past, he sort of mistake that goobers me up once every five years or so.
Never assume that UNIX command line tools are compatible with the awesome GNU versions
I encountered this “problem” on my first few Sun Solaris servers. It was painfully obvious there during interactive shell sessions. I was constantly hitting command line switches that didn’t exist. I’ve always preferred the long form “double dash” command line arguments over their single letter equivalents, but at the time those were seemingly nonexistent on Solaris.
This time, though, my problem was that the BSD version of the sed
command doesn’t support the -r
command line switch like the GNU version does. This is required to support the regular expression capture groups that most of zsh-dwim’s transformations use.
The solution
The solution in Lepht’s pull request was to use the gsed
command if it was available. This worked great, so I immediately accepted the pull request. I didn’t give it much thought again until I was updating the documentation.
I originally decided to use sed
for the regular expression substitutions in zsh-dwim because sed
is very lightweight, and the regular expression substitution engine built into zsh
didn’t feel up to the challenge. I also figured that everyone would have it installed.
That last part was very important. I realized as I was updating the documentation that I didn’t want anyone to have to go through the trouble of installing additional software to use zsh-dwim. I talked to Lepht about the idea of swapping out sed
for perl
, since pretty much every UNIX flavor has Perl installed already.
I think we came up with a pretty reasonable solution. zsh-dwim will now use sed
if it supports the -r
argument. If not, it will check and see if gsed
is installed. Failing that, it will just use perl
. That means it should use the most efficient regular expression engine available, and most people won’t have to think about it at all.
Many thanks to Lepht for pointing out my oversight!