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.