POSIX Shells: compatibility or preference?

POSIX compliance: just preference or necessity? I explore the importance of shell compatibility in my environment, from NuShell to Bash and ZSH. What's the real impact of not being POSIX compliant?

POSIX Shells: compatibility or preference?
Photo by MJH SHIKDER / Unsplash

If you are reading this, you might fall into 2 categories: “What is POSIX?” and “How can it be just preference?”. For both groups I believe we need to review of what POSIX is and what it means.

So first, let’s define what is POSIX: Portable Operating System Interface. The standards of how we can make the interface for the user feels naturally ”the same” for using OS APIs and utilities. This includes the shells and how they should interact with the Operational System.

I have recently changed my default shell to NuShell, far from a POSIX compliant shell, but one that feels great to use. It has the perfect mix of what I love from PowerShell and still feels much more powerful and similar to Bash or ZSH - even though there are no similarities. This Prompted me to ask myself: do I need a POSIX compliant shell?

If we dive deeper, we must ask ourselves: why the Shell? Why not stop at the OS API layer and let developers use those for developing whichever tools they need for that and distribute their binaries that use them with the Operational System? That is my question. It is clearly answered by then looking at my environment and seeing there’s plenty of software that I install and run that depend on SH or Bash to install or run. But again I see an issue: not All those scripts truly work the same depending on the shell I’m using. My MacBook used to run ZSH as the default shell, and it could not use some of those scripts as they were. So I just used the shebang to run Bash instead. Wait. Bash? In my environment that I use ZSH as the default?

So, do I really need to worry about my local shell being POSIX compliant if I can just have Bash run what it needs and then have a “better“ shell for my interface? What do I need for my terminal? What do YOU need for your terminal? Well, it depends.

First, let's address why you might want to use a shell like Bash: portability, big install base, similarity with other shells, and stable. If all you want is your scripts to run everywhere, the promise of POSIX compliant shells is quite enticing; you write a script once in any machine - say Ubuntu - and then run that in any other machine and shell, like MacOS and ZSH with minor tweaks. Quite powerful to have this interoperability as a given.

If that's the case and it's available in all environments that you might need, why would you not want to use it? That is the root of the issue: not all environments are made the same. Not all Shells are made the same. You might want to install the tiny alpine image that comes with Ash, or the mainstream distribution of Ubuntu with Bash, you might even want to buy those fancy MacBooks everyone is talking about that comes with ZSH. Each of those have a different shell, and even though those were made to be POSIX compliant, each of those have differences that are bound to make you scratch your head more than you would believe me. So instead of telling you, let me show you!

Using arrays in ZSH
I have the following statement: TOKENARRAY=($TOKEN) $TOKEN is a numeric variable. If I try this: echo ${TOKENARRAY[0]} it shows me an empty string. If I do: echo ${TOKENARRAY:0} it shows me…
ash and bash glob wildcard expansion gotcha

Does that mean that those shells are not compliant? Not really, it just means they follow the rules; but they still implement their own preferences. So they not necessarily are as “portable” as we'd first expect them, but I do not believe the standards are at fault. If you look closely, those are here to help the portability of users and scripts, but not compatibility. It might just be me, but I had the impression of compatibility in mind when I first started thinking about this, and I was probably wrong.

So we arrive now at the crossroads of the title: Compatibility or Preference? I cannot say compatibility is ever the intention here, but rather ease of use and portability of most things the Operational System needs to work. Think about that next time you feel weird about a non-POSIX shell or utility. You would probably be surprised with the tools you would not try before.

It is not to say you cannot use those non-compliant tools for your own benefit. NuShell and PowerShell Core are cross platform available, and will give you the same experience in most Operational Systems. For that, I am here to say: choose what you believe is best for the time and context. It all truly depends.

In conclusion, don’t just choose one and forget about the others. Be aware of the differences and learn how to use the available tools out there. You never know when you will be thrown into a machine with only Ash and VI to troubleshoot an issue, but you can always use your preferred environment when in the comfort of your machine.

I for one am quite happy to call NuShell my new "home" shell.