8

How can I test a shell script to verify that it is POSIX compliant? I have a large set of scripts written for various versions of Bash and possibly other shells. I'd like to be able to determine which are fully POSIX compliant and which are not. Ideally, I'd like to find something like lint but for shell scripts.

  • 1
    Although it makes no claims to test for complete POSIX compliance, a commonly-used script along these lines is [`checkbashisms`](http://sourceforge.net/projects/checkbaskisms/). It tests for and reports on non-portable syntax. – John1024 Feb 22 '15 at 06:09
  • 1
    Use #!/bin/ksh as a default, so you will avoid things that are easier in bash. It is only a start, you must also skip Linux improvements like `sed -i` – Walter A Feb 22 '15 at 10:15
  • Possible duplicate: http://stackoverflow.com/questions/3668665/is-there-a-static-analysis-tool-like-lint-or-perlcritic-for-shell-scripts – Thomas Dickey Feb 27 '15 at 11:24
  • Closely related; possible duplicate. http://stackoverflow.com/q/11376975/1301972 – Todd A. Jacobs Sep 28 '15 at 17:25
  • Asked and answered: [How can I test for POSIX compliance for shell scripts?](https://unix.stackexchange.com/questions/48786/how-can-i-test-for-posix-compliance-for-shell-scripts) – Todd A. Jacobs Sep 28 '15 at 17:37
  • See [Convert Bash Scripts To Shell](https://stackoverflow.com/q/33394706/4154375). – pjh Apr 15 '22 at 00:24

1 Answers1

2

It's not possible to statically verify if a script is POSIX compliant because the differences are not restricted to syntax changes and some differences simply changes the way some commands behave. This link describes the differences between bash and POSIX standard.

However, you can explicitly check your scripts running then with bash --posix ./your_script.sh or using set -o posix inside them.

Marcelo Cerri
  • 162
  • 1
  • 10
  • Bash's POSIX compliant mode is unfortunately not POSIX compliant. See https://tiswww.case.edu/php/chet/bash/POSIX and the end of the URL you posted. At the very least, you also need to enable the 'xpg_echo' option to get POSIX compliant "echo" behaviour. There are other things as well. I suggest using dash for posix compliant testing. – Ferry Boender Jan 16 '17 at 14:43