Which is the most portable of sed, awk, perl and sh?

Which is the most portable of sed, awk, perl and sh?

sed, sh and awk are portable being specified by POSIX, perl is not as not being backed by a standard.

Can someone put these tools in order of portability?

If you stick to compliant code, there should be no order of portability for the three POSIX commands.

Which of these is certain to be found on even the most minimal *nix systems?

The three POSIX ones along with many other utilities are mandatory for an OS to be POSIX. OSes lacking some of them due to minimization, or providing incomplete/non-conforming implementations do exist though.

Actually, most (if not all) free and open source Unix like operating systems probably would not pass the conformance process should they try to, and they never try anyway.

Is any of them 100% sure to be present?

I would be surprised to find a *nix like OS lacking a Bourne syntax based shell, but anything is possible, especially with embedded systems.

My guess is that the order is the following: Some shell will be present as the default, will that always be at /bin/sh?

/bin/sh is likely to be a Bourne syntax family shell but is not guaranteed to be POSIX compliant, even en POSIX compliant systems. For example, it is /usr/xpg4/bin/sh on Solaris 10 and older while /bin/sh is the legacy original Bourne shell which is not POSIX.


Take a cue from the Autotools: stick to the lowest common denominator of Bourne and POSIX shell — possibly augmented by sed — if you have to write something that must work everywhere. There may exist systems where something breaks, but you can work around such problems by rewriting.

For example, some ancient systems have problems with expansion errors in test, a.k.a. [:

 if [ $foo = bar ] ; then...

so the Autoconf practice is to rewrite it in double quotes with a single character prefix, like so:

 if [ x"$foo" = "xbar" ] ; then...

You could also use "x$foo" here. This guards against the possibility that $foo could be a valid option to test(1), and since [ is an alias for test, it could misinterpret the expression. The solution is to set up a situation where the unknown argument to [ always begins with x, which means it cannot have special meaning to [.

(Autoconf also recommends using test instead of [, but that advice comes about as a reaction to possible conflicts with M4, which also uses [ in its syntax.)

awk is POSIX, so theoretically it is available everywhere. It is even in Busybox, so you will have an awk implementation even in some very restrictive embedded Linux systems. Still, I'd be less surprised to come across a system without awk than sed. I suppose it comes down to complexity: simpler tools are more likely to survive aggressive triage.

Perl isn't part of any widespread standard, POSIX or otherwise, so you simply cannot count on it if you know nothing in advance about the target environment. Perl is not installed by default in:

  • Cygwin
  • FreeBSD and NetBSD
  • "minimal" installs for some Linuxes, including Slackware
  • many embedded Linuxes that rely primarily on Busybox for their userland

The Autoconf manual has a chapter on portable shell programming which should be helpful to you. The last section covers tools like sed, awk, and many others.