Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What resources exist for portable shell programming? The ultimate answer is to test on all targeted platforms, but that's rarely practical.

The POSIX / Single UNIX specification is a start, but it tells neither you what the level of support of each implementation is, nor what common extensions exist. You can read the documentation of each implementation, but that's very time consuming and not completely accurate.

I seems to me that an ideal format would be some kind of community-annotated version of the POSIX spec, where each feature is annotated by its support level amongst the different implementations. Is there such a thing? Or are there other useful resources?

For example, there is Sven Mascheck's shell portability pages, but it's only about syntactic elements and a few built-ins, and only covers old shells. I'm looking for a more comprehensive resource.

share|improve this question
2  
N.B. Please don't answer here just to cite one particular implementation's conformance document. If you have one of those, the corresponding tag wiki would be a good place. –  Gilles Mar 23 '11 at 21:57
1  
Someone needs to mine the revision history for autoconf and Metaconfig (Perl, rn) and collect all the tricks and their explanatory comments in one place. –  geekosaur Mar 23 '11 at 22:01
    
@geekosaur: Good suggestion. I've never looked at the internals of autoconf, is there something one could use as a guide when writing one's own scripts? If so, this would be a good answer to my question, even if it's not a definitive one. –  Gilles Mar 23 '11 at 22:03
5  
I'm sure you both could find it easily, but for the benefit of other people who are following this thread, here's a link to a relevant page from the autoconf manual: gnu.org/software/hello/manual/autoconf/Portable-Shell.html –  jrtayloriv Mar 24 '11 at 8:50
1  
show 4 more comments

5 Answers

up vote 22 down vote accepted

The autoconf manual has a section on portable shell programming.

Although that's not specifically targeting POSIX, it's probably the most complete collection of what to do and not to do when attempting to write portable shell code.

share|improve this answer
2  
That's one of the best resources and was built while writing code in M4 that had to produce shell code that was portable across as many shells as possible. –  Tim Post May 31 '11 at 23:28
add comment

In addition to dash and posh, there's bournesh (or bsh), the Heirloom Bourne Shell, that can be used to detect Bashisms.

The Heirloom Project also includes "The Heirloom Toolchest", a collection of more than 100 standard Unix utilities (which could serve as a starting point for comparing command line options).

share|improve this answer
add comment

Writing your scripts using dash might be a start.

share|improve this answer
2  
Testing with dash is the bare minimum to avoid accidental reliance on ksh/bash features, but I'm after more than that: knowing about deficiencies in other shells (e.g. zsh's poor trap handling), and above all in shell utilities (e.g. OpenBSD's find still hasn't implemented -exec +). –  Gilles Mar 24 '11 at 8:18
add comment

Similar to this answer, try executing your script in posh.

Also, don't forget to set the POSIXLY_CORRECT environment variable to true, this causes many programs (not only the shell) to adhere more strictly with the POSIX standards.

share|improve this answer
add comment

To a little extend, you can try checkbashisms in Debian/Ubuntu's devscripts package.

It is not perfect, but it has the benefit of being an existing starting point. For example, it doesn't see the classical glitches with sed/find concerning the GNU vs BSD/other differences.

By default, it is Debian+dash oriented, the -p flag can be useful for in your case.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.