How can I test for POSIX compliance of shell scripts?

Considering that POSIX is the closest thing to a common standard among 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 mentioned 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.

Asked By: rahmu


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!

Answered By: Nicholas Wilson

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

          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.

Answered By: James Sneeringer

You can use ShellCheck (GitHub) as a linter for your shell scripts. There is also an online version.

To detect POSIX compatibility issues (e.g. SC2039), the shebang line of your shell script should be #!/bin/sh. You can also pass --shell=sh to shellcheck.

Example (

if [[ $HOSTNAME == test ]]; then
    echo fail &> foo

Result (shellcheck

In line 2:
if [[ $HOSTNAME == test ]]; then
   ^-- SC2039: In POSIX sh, [[ ]] is undefined.
      ^-- SC2039: In POSIX sh, HOSTNAME is undefined.    

In line 3:
    echo fail &> foo
              ^-- SC2039: In POSIX sh, &> is undefined.
Answered By: johnLate

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?

Dash fits the bill here.

DASH is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. It does this without sacrificing speed where possible. In fact, it is significantly faster than bash (the GNU Bourne-Again SHell) for most tasks.

When writing shell scripts that should conform to the POSIX Shell Command Language, I use Dash as the interpreter when testing my scripts as it implements only POSIX shell features and throws an error if it encounters any Bashisms such as array variables or process substitution.

Dash is a port of the Almquist shell (hence its name, Debian Almquist shell) and on Debian-based systems, /bin/sh is a symbolic link to /bin/dash. For more information, see Almquist shell – Wikipedia.

Answered By: Anthony Geoghegan
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.