Alex Rivera | Logout

Is there a minimally POSIX.2 compliant shell?

Asked 2012-07-07T17:01:37.367
32

Is there a minimally POSIX.2 compliant shell (let's call it mpcsh) in the following sense:

if mpcsh myscript.sh behaves correctly on my (compliant) system then xsh myscript.sh will behave identically for any POSIX.2 compliant shell xsh on any compliant system. ("Identically" up to less relevant things like the wording of error messages etc.)

Does dash qualify?

If not, is there any way to verify compliance of myscript.sh?


Edit (9 years later):

The accepted answer still stands, but have a look at this blog post and the checkbashisms command (source). Avoiding bashisms is not the same as writing a POSIX.2 compliant shell script, but it comes close.

Edit
Report

2 Answers

34

The sad answer in advance

It won't help you (not as much and reliably as you would expect and want it to anyway).


Here is why.

One big problem that cannot be addressed by a virtual "POSIX shell" are things that are ambiguously worded or just not addressed in the standard, so that shells may implement things in different ways while still adhering to the standard.

Take these two examples regarding pipelines, the first of which is well known:

Example 1 - scoping

$ ksh -c 'printf "foo" | read s; echo "[${s}]"'
[foo]

$ bash -c 'printf "foo" | read s; echo "[${s}]"'
[]

ksh executes the last command of a pipe in the current shell, whereas bash executes all - including the last command - in a subshell. bash 4 introduced the lastpipe option which makes it behave like ksh:

$ bash -c 'shopt -s lastpipe; printf "foo" | read s; echo "[${s}]"'
[foo]

All of this is (debatably) according to the standard:

Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment.

I am not 100% certain on what they meant with extension, but based on other examples in the document it does not mean that the shell has to provide a way to switch between behavior but simply that it may, if it wishes so, implement things in this "extended way". Other people read this differently and argue about the ksh behavior being non-standards-compliant and I can see why. Not only is the wording unlucky, it is not a good idea to allow this in the first place.

In practice it doesn't really matter which behavior is correct s

answered 2013-05-04T15:41:34.313
6

Probably the closest thing to a canonical shell is ash which is maintained by The NetBSD Foundation, among other organizations.

A downstream variant of this shell called dash is better known.

answered 2013-01-15T15:49:14.177

Your Answer