The POSIX shell and the three sorts of Unixes
Avery Pennarun recently wrote Insufficiently known POSIX shell features, where he talked about a number of nice shell things that are not Bash but are instead POSIX shell features. Although he footnoted this in his entry, I want to draw your attention to how there are three sorts of Unix machines (or, well, Unixes):
- machines on which
- machines on which
/bin/shis POSIX-compatible but is not Bash.
- machines which have a POSIX-compatible shell, but it is not
Every so often some well intentioned person attempts to transition the first sort of Unix into the second sort. Busy sysadmins usually immediately reverse the transition because we have better things to do with our time than debug Bashisms that have crept into administrative scripts or, worse, explain to users why their shell scripts just broke and how no we are not going to do anything about it although we could because it is good for them, honest.
(Rewriting shell scripts to pointlessly avoid Bashisms is the very opposite of productive work. It's even less productive than browsing Slashdot, because there is some vague chance that you could learn something from Slashdot.)
The third sort of Unix is a pain in the rear for everyone. In many
respects it might as well not have a POSIX shell, because you can't
easily use it in portable scripts. If you are a big project like
redo you can work around the
difference and find yourself the right shell, but if you are an ordinary
person writing cross-machine shell scripts, ones that you want to run
without an installer step, well, you lose. Your scripts all start with
#!/bin/sh' because that's the only reliable cross-Unix name for a
Bourne shell, so you can't count on POSIX features.
Fortunately the third sort of Unix is mostly dying out. The largest holdout in our environment is Solaris, which we don't let users log on to and barely run anything on. Even then, the differences sometimes get to us.
Honestly, I suggest that you ignore the third sort of Unix unless you can't because you have one. And if you want to write portable POSIX shell scripts, make sure that you use the second sort of Unix right from the start. (Or that someone involved in the project does.)
Comments on this page:Written on 02 March 2011.