I've come across various shells aside from Bash, and I've noticed many of them are labeled as "POSIX-compliant." I'm curious about the differences between scripting in Bash versus a strictly POSIX-compliant shell like Dash. I understand that in Bash, you have these things known as 'Bashisms'—elements specific to Bash that won't work in every shell. In contrast, a POSIX-compliant shell should work across any compliant system. So, my questions are: Is there a reason to use Bashisms? Are there features in Bash that a strict POSIX shell would lack? Also, it seems challenging to write a fully POSIX-compliant shell script when most tutorials focus on Bash.
4 Answers
One major difference is that Bash supports arrays, while POSIX shells like Dash do not. If you need arrays in your scripts, that's a pretty big deal. The other differences are often about convenience, but recreating array-like behavior in POSIX can be quite tricky.
Totally agree, it feels like some limitations in POSIX really stem from past design choices.
There are definitely gaps, and I've faced some issues at work because of it. For example, in our setup, `/bin/sh` was linked to Dash instead of Bash, which led to some scripts failing because the vendor assumed Bash was being used. In general, sticking to POSIX-compatible scripts is a safer bet for portability, though I understand the struggle since most resources out there use Bash.
Using Bashisms definitely means you're missing out on the compatibility of strict POSIX shells. If you’re worried about adhering strictly to POSIX, tools like Shellcheck can highlight Bashisms in your scripts and suggest alternatives. It's a good way to write portable scripts while learning more about the differences.
And don’t forget to test against Dash for those POSIX-only checks, it's almost synonymous with being strict POSIX.
The POSIX standard outlines a basic set of features, which ensures compatibility across compliant shells. If you stick to those basic features, your script will work anywhere that's POSIX-compliant. However, some find that those minimal features can be limiting. If you care more about functionality (like arrays or advanced redirections), Bash is often the better choice. It's generally more feature-rich than POSIX Sh, so it all comes down to whether you prioritize compatibility or functionality.

You can use things like `$@` together with quite a few subshells, or construct carefully formatted strings, but it's definitely not straightforward!