๐งช Cross-Platform Shell Script Testing
Writing truly portable shell scripts requires testing across various platforms and shells. Here's how to set up an effective testing environment.
๐งฉ Why Test Across Platforms?
Even small differences in tools (grep, sed, find) or shell behaviors can break scripts unexpectedly.
Common issues include:
- GNU vs BSD utilities (especially on macOS)
- Different parameter expansion support
- Variants of test and [[]]
- Default shell version differences
๐งช Methods for Testing
1. Docker Containers
Use containers with different base images:
| Image | OS Variant | Default Shell |
|---|---|---|
alpine |
BusyBox/Linux | ash (POSIX) |
ubuntu |
GNU/Linux | dash/bash |
debian |
GNU/Linux | dash/bash |
centos |
Red Hat/Linux | bash |
rockylinux |
Community RHEL | bash |
Example:
1 2 | |
You can mount current directory and test directly inside:
1 | |
2. Virtual Machines (Vagrant/VirtualBox)
Ideal for testing full OS behavior including init systems, filesystem permissions, service managers.
Create a Vagrantfile:
1 2 3 4 5 6 7 8 9 | |
Run:
1 2 | |
3. Cloud-Based Sandboxes
Quick online testing without local setup:
- GitHub Codespaces
- Gitpod.io
- Repl.it (supports multiple shells)
Great for quick experiments and sharing reproducible test cases.
4. Using ShellCheck Locally
Install ShellCheck for static analysis:
1 2 3 4 5 6 7 8 | |
It catches common mistakes even before running the script.
๐งพ Recommended Practice Workflow
- Write initial draft locally.
- Validate syntax with
shellcheck. - Run in Docker (
alpine,ubuntu,debian). - Optionally test on real hardware or VM.
- Commit only after verifying portability.
๐งช Bonus: Quick POSIX Compliance Check
Test your script against multiple POSIX-compliant shells:
1 2 3 4 | |
๐ Now proceed to: Basics - Shell 101