Note: This POSIX compatibility check uses static analysis to catch common portability issues. It is not a full shell parser. For critical production scripts, also test with dash, ash, BusyBox sh, and ShellCheck.
Overview
While Bash is popular, many minimal environments (like Alpine Linux containers, BusyBox, and embedded systems) do not have Bash installed. POSIX-compliant /bin/sh scripts guarantee maximum portability across virtually any Unix-like system.
POSIX Rules of Thumb
- Use `[ ]` instead of `[[ ]]`: The double-bracket test is a Bash extension.
- Use `=` instead of `==`: In POSIX `[ ]` tests, string equality is checked with a single equals sign.
- Avoid Arrays: POSIX shell does not support arrays. You must use positional parameters (`$1, $2`) or delimited strings.
- Use `.` instead of `source`: `source` is a bashism. Use `. /path/to/file` to load variables.
- Avoid the `function` keyword: Declare functions using `name() {` instead of `function name {`.
Frequently Asked Questions
What is a 'Bashism'?
A Bashism is a syntax feature exclusive to Bash (like [[ ]] or arrays) that will cause a syntax error in stricter, minimal shells like dash or BusyBox ash.
Why use /bin/sh instead of Bash?
Using /bin/sh is mandatory for Alpine Docker images and heavily reduces the footprint of the runtime environment. It forces you to write cleaner, more standard shell code.
Does set -eu work in POSIX?
Yes, set -e (exit on error) and set -u (exit on unset variable) are fully POSIX compliant. However, set -o pipefail is NOT POSIX and will fail in many shells.