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.

Probably the main thing that this website has taught me so far is the importance of writing portable shell scripts.

Considering that POSIX is the closest thing to a common standard between all unices, I'm interested in knowing if there's a shell that supports it exclusively. While most modern shells provide support for POSIX (and will run POSIX compliant scripts without any problem), they don't do a good job at pointing out non compliant features.

Is there any shell that implements POSIX and POSIX only, in such a way that it'd throw an error for any non compliant feature?

EDIT I want to clarify that I'm not asking for general tips for writing portable shell scripts. The related question mentionned in the comments already covered this. I thought of this question when I found out that bash has a --posix option but only to discover that it only affects some intialization behaviors which is not exactly what I'm looking for.

share|improve this question
    
Related: Resources for portable shell programming –  Gilles Sep 21 '12 at 13:13
    
@Gilles: Maybe I should mention that I did come across this question but only one answer suggested testing with dash. I mentionned portability as a general context for my question but that was not its true intent. –  rahmu Sep 21 '12 at 13:22
    
Sure, I wanted the two questions to be linked because they are likely to be of interest to the same people. They aren't duplicates by any means. By the way, posh is a better test for POSIX compliance than dash. –  Gilles Sep 21 '12 at 14:05
add comment

2 Answers 2

up vote 9 down vote accepted

Unfortunately, 'portable' is usually a stronger requirement than 'POSIX-compliant' for shell scripts. That is, writing something that runs on any POSIX shell isn't too hard, but getting it to run on any real-world shell is harder.

You can start by installing every shell in your package manager, in particular debian's posh sounds like what you want (Policy-compliant Ordinary SHell). Debian's policy is POSIX with a few exceptions (echo -n specified, local...).

Beyond that though, testing has to cover a few shells (/bin/sh especially) on a range of platforms. I test on Solaris (/bin/sh and xpg4/sh), and BSD. AIX and HP-UX are very compliant and don't cause problems. bash is a little world of its own.

I'd recommend the Autoconf guide to portable shell, which is absolutely brilliant and saves a lot of time. Large chunks of it are obsolete, but that's OK; just skip TruUnix and Ultrix and so on if you don't care!

share|improve this answer
    
posh does indeed sound like what I'm asking for. I'll do some tests as soon as I can. –  rahmu Sep 21 '12 at 13:29
    
I answered before spotting the related question! posh is a debian thing, so won't be packaged on every system. Also, the shell isn't necessarily the thing to be most worried about; sed incompatibilities are a big problem, for example. –  Nicholas Wilson Sep 21 '12 at 13:47
    
I wouldn't bother porting to Solaris /bin/sh aka Bourne shell (which is not POSIX). Solaris like all modern unices has a POSIX shell, it just happens not to be in the usual location (/usr/xpg4/bin/sh) –  Stéphane Chazelas Sep 21 '12 at 14:31
    
Sadly, we ship scripts that have to run on at least every /bin/sh. It's not as bad as all that... I'd rather not have to do it though. –  Nicholas Wilson Sep 21 '12 at 14:52
1  
The reason I treat /bin/sh as important is because it's invoked from the shebang in so many scripts. "/usr/bin/env sh" is hardly an improvement. If you're going to put any effort into making something work on non-POSIX shells, I feel I may as well prioritise each system's /bin/sh. It won't be long before some customer runs your script with the default shell, which is not all that unreasonable. –  Nicholas Wilson Sep 21 '12 at 17:07
show 2 more comments

Bash will run in POSIX-compliant mode if the POSIXLY_CORRECT environment variable is set. From the manpage:

   POSIXLY_CORRECT
          If  this  variable  is  in the environment when bash starts, the
          shell enters posix mode before reading the startup files, as  if
          the  --posix  invocation option had been supplied.  If it is set
          while the shell is running, bash enables posix mode, as  if  the
          command set -o posix had been executed.

Many other GNU utilities will also honor POSIXLY_CORRECT, so if you're on a system with predominantly GNU tools (e.g. most Linux systems), this is a good start if your goal is POSIX conformance.

share|improve this answer
3  
Even in POSIX mode, bash will still allow some non-POSIX features such as [[. –  jordanm Sep 21 '12 at 23:59
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.