2

If I wanted to be sure that a script I have written is as portable as possible and only uses POSIX features and no, say, bash features, is there an explicit way to check this?

Obviously, I could try running it in a different shell to my usual shell, but that only confirms that my script is limited to features supported by that shell... features that, themselves, might go beyond POSIX.

2 Answers 2

3

The POSIX mode of BASH, easily run:

bash --posix /PathToYourScript

to get in it and run your script there.

It should give you a pretty clear idea of if your Script is POSIX- compatible or not.

From what I read, DASH, which is standard (sh is a symbolic link to dash) on Ubuntu / Debian and derivatives is a very strict POSIX shell, which may be worth trying, because it's even stricter than BASH in POSIX mode.

1

An alternative approach to Tim Schumacher's answer would be to copy paste the script to ShellCheck.net, only changing the Shebang to #!/bin/sh, and carefully studying the output.

This approach may be even better in case you are coding something and you are looking not only for non-compliance with POSIX, but also for other errors and suggestions. You will be impressed especially, if you use it on daily basis.


Of course, you can do:

bash --posix /PathToYourScript

But from my experience, DASH, which is standard on Ubuntu / Debian and derivatives (sh is a symbolic link to dash), is a very strict POSIX shell, and even stricter than BASH in POSIX mode.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .