Your favorite shell in FreeBSD

My (limited) understanding: If a shell can pass all of the Posix compliance tests, it is Posix conformant. None of those tests use non-Posix features, so a shell that has additional features could pass. Let me give a really simple example: A normal shell has commands such as set, if, for. A new shell might have a command called "foo", which when executed would cause purple elephants to fly around your screen. Since none of the tests will execute foo, the new shell would pass the test.

The other bit of advice: The way to write scripts that use ONLY Posix features is not to write them on some shell that is rumored to be Posix conformant, and then if the scripts work, they must be Posixly correct. Instead, as you write the scripts, have a book or printout of the Posix standard next to you, and use it as a reference. O'Reilly used to have really good books about the Posix C interfaces (Posix.1 and Posix.4); I suspect there are good books about the Posix shell too.
 
ralphbsz mostly agree.

The fact that a shell can do more than specified by POSIX doesn't automatically make it non-compliant. And additional commands are trivially safe, for most commands it's unspecified whether they are builtins or external utilities, and additional utilities in your PATH, where the shell would find and execute them, are always allowed, for obvious reasons.

Extending the syntax of commands/utilities that *are* specified by POSIX isn't that trivial any more. The example above was about test, I would conclude it is safe because the POSIX specification for test just states something like "anything else has undefined results", IIRC. But say it would require something like a diagnostic about "invalid" syntax, and you'd immediately have a compliance issue with your extension.

As for how to write portable scripts, I also agree: Refer to the standard! I think the documents you'll find published online, like e.g. Shell Command Language (and all the descriptions of shell utilities also found there), should suffice. Still, it's sometimes easy to overlook something, therefore testing in actual shells is also a part, ideally a few different implementations. That's where I found pbosh(1) from shells/bosh pretty helpful, it claims to do "POSIX and nothing else", and it indeed exposed an issue for me before.
 
I find writing portable shell scripts as an exercise in futility. Try writing a portable shell script for finding setuid binaries and you will find that GNU find doesn't support the '+' sign in `-perm +06000` using '/' instead.

So I use Bashisms most of the time because I need true lists and associative arrays.

We have other scripting languages for writing scripts that are intended to be portable.
 
Hi ralphbsz, zirias@,

thank you for the replies.

My question re the compliance was really about how it affects writing scripts. Your advises essentially solve my inquiry, regardless of the strict definition of POSIX compliant shell.

Kindest regards,

M
 
Back
Top