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.

POSIX Shell Script Generator

Generate highly portable /bin/sh scripts that work seamlessly on Alpine, BusyBox, macOS, and minimal containers.

Typically /bin/sh for maximum portability.

- Fails on any error and unset variables. (POSIX does not support pipefail).
- Adds a portable trap to catch unexpected exits.
- Avoids polluting global scope.

Your core POSIX shell commands.

script.sh

Usage Instructions

# 1. Save the file

nano script.sh


# 2. Make the script executable

chmod +x script.sh


# 3. Run the script explicitly with sh

sh script.sh


# 4. (Optional) Run ShellCheck to verify POSIX compliance

shellcheck -s sh script.sh

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.