* well, except for the fact that MacOS defaults to zsh now.
I also write scripts for both Linux and macOS. They both provide bash, though macOS ships with an older version of it. So I use #!/bin/bash and stick to the older version that macOS provides. (Which often means ignoring the specific recommendations from shellcheck. e.g., macOS bash doesn't have readarray.)
What I have to be more careful of is differences in utilities like find, sed, etc.
1. Originally from RTFM -> RTFA -> TFA, so feel free to use your own substitution for F, but in this case it's a fine article.
I think it's more broadly applicable
But Zsh is also POSIX sh compatible, is it not?
Afaik it can be thou
Zsh:
$ A="foo bar baz"; for X in $A; do echo $X; done
foo bar baz
Dash: $ A="foo bar baz"; for X in $A; do echo $X; done
foo
bar
bazor use 'emulate sh' to enable many flags at once.
(and I personally would prefer if other shells would behave like Zsh and not word split unquoted variables by default)
The major factors when it comes to portability are, for all of them, that (a) the POSIX-conformant mode has to be specially invoked, and (b) even if invoked in conformance mode the non-POSIX parts are not always turned off. It's not that the shells won't handle POSIX-conformant scripts. They even mostly will in their (default) non-conformant modes. It's rather that it's difficult to discipline onesself to keep within the bounds of POSIX conformance when writing, even if one tests with conformance mode switched on.
The Debian Almquist and Watanabe shells are the best at preventing non-POSIX stuff creeping in by accident, because the former doesn't have much of it in the first place and the latter actually does things like turn off non-POSIX option arguments and builtins so that they generate errors in conformance mode and one can detect accidental "Yashisms" in testing. With the others, it's easier to accidentally use some of their various extensions and not spot it without explicitly testing with something in addition to the shell in its POSIX-conformant mode.
I've written hundreds of scripts for Busybox for Windows (https://github.com/rmyorston/busybox-w32), Linux bash and macOS zsh. POSIX compatibility means a lot.
WRT the specific concerns of arguments: if you are doing something complicated, use Python. ChatGPT writes it better anyway.
In my experience, more complex scripting is reinventing Gradle, Ansible, Terraform, YAML & similar.
These are all just tools. They each have their strengths and weaknesses.
There are times when shell is the best tool for the job and when it is, I prefer to use bash.
If I have to write POSIX shell because it's a constrained environment, sure, I can do it. It's just been something that's rarely been a requirement.
Amusing that this is even a factor
Introducing Python syntax for this reason alone is way over-engineering your script.
Yeah, if you're doing something that requires better data structures[1] then don't use the shell, but switching to Python just to call existing programs makes things more complicated, not less.
[1] Arrays, Trees, Dictionaries, etc
…at the cost of making it harder to grok for other people what that code is doing, and having them wonder if there’s some other important reason it has to be written that way.
Same applies to Stack Overflow answers and blog posts. If you’re teaching someone how to do something, always use the long flags so they can better understand it as they read. If you don’t know what the long option is, take the two seconds to check the manual.
https://www.shellcheck.net/
(And I also like using shfmt on top of that.)
FWIW, I find writing POSIX-compliant shell rarely necessary. In my 25 years of writing shell scripts, they've nearly always been for automation on a specific OS, so portability was not ever a concern. These days I almost always use "#!/bin/bash" and only need to take care about the version of bash the system offers.
When I do have to write a script for more than one OS (e.g., macOS and Linux), I'll typically have bash on both systems, so I make use of bash'isms because they frankly make writing shell a lot more pleasant. I do need to take care however to use only those features supported by the older version of bash that ships with macOS.
Where I have to be more careful is in utilities like find, sed, etc which differ between BSD and Linux.