by augustk · 3y ago · 0 comments
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
Your script will use sh if it starts with #!/bin/sh (instead of #!/bin/bash).
I can also recommend ShellCheck which is a shell script analysis tool (implemented in Haskell) which can find errors and potential problems in your scripts. On a Debian system, installing ShellScheck is as simple as `sudo apt install shellcheck'.
I don't buy this argument. Ok, it's a standard, but sometimes it's a pain-in-the-hole standard. Bash augmentations lessen some of the pains, and it's just nicer. The only situation is if you're using busybox in a very limited system or you reeeeeally need your script to run on many difference unices, which let's be honest, is not that common nowadays.
The Debian people were concerned, for starters, with how much time the Bourne Again shell spent, at process initialization, setting up things for extensions and interactive features that were never employed in non-interactive "sh" mode; a significant cause for concern given how much of the system was executable shell scripts.
Curious, what are the 3 things ?
But yes, people should always use sh (or ksh) for scripting as opposed to bash, why, it is far more portable to other systems.
* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=294962
They've added 2 more since.
* https://www.debian.org/doc/debian-policy/ch-files.html#scrip...
Strictly speaking, there are other differences between the Debian Almquist shell and a truly SUS conformant sh.
* https://unix.stackexchange.com/q/697007/5132
But the aforementioned are what Debian people explicitly wanted and couldn't live without. Note that the StackExchange answer is a comparison of a 2022 dash to a 2017 standard, neither of which existed at the time. Debian Policy, the Debian Almquist shell, and the POSIX standard have all been revised since then.
Due to docker, minimalistic Linux'es have been more common, including trimmed-down versions of distros that would otherwise feature bash.
Both sh and bash are terrible, ugly hacks. Anything that requires more than 3 lines of them (including the #! line) should be written in a proper scripting language.
Though I do agree that cross-platformity to that level is rarely meaningful, and so things like python are just as likely available/can be made available.
I'm not sure there is a good alternative to sh/bash shell scripts as most dynamic languages have become pretty large dependencies these days.
...that said, having to support bash 3.2 for MacOS is a horrible thing.
But I know that you meant it as a rhetoric, so my non-rhetorical answer would be that python3 is almost universally available on distros that are not minified deliberately (containers).
Which version of Bash?
ShellCheck is absolutely the most important thing to use when writing scripts. Each time that you get a warning that you don't recognise is a learning opportunity - just follow the link provided in the warning. Also, take the time to add the "# shellcheck disable=" comments so that scripts don't issue any warnings from shellcheck.
I also recommend visiting https://mywiki.wooledge.org/BashFAQ for avoiding the many footguns, especially with filename parsing.
I always use $(...) or rather "$(...)" instead of backticks.
Command substitution shall occur when the command is enclosed as follows:
$(command)
or (backquoted version):
`command`
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...POSIX also has $(command) for command substitution. It can certainly be annoying when it's not used, though. I think shellcheck will recommend it over backticks.
Edit: After looking for a problem with POSIX, I found this obscure issue that may well catch out people as under POSIX, IFS is a terminator rather than a separator: https://mywiki.wooledge.org/BashPitfalls#IFS.3D.2C_read_-ra_...
Nah I like using bash features and all the systems that I care about have /bin/bash so it doesn't matter.
It would be much more correct to say that bash's features are a superset of the shell features standardised by the POSIX standard, as is the case for several other shells (each adding their own parts on top of the standard) too.
or as I've taken to calling it, Ba + sh
Meaning /bin/sh is not more portable because you can’t be sure which shell will be used.
Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh, and should be determined by interrogation of the PATH returned by getconf PATH, ensuring that the returned pathname is an absolute pathname and not a shell built-in.
I don't know how this trend started but now it's been cargo-culted to death. Same with the /usr/bin/env thing in shebangs. shellcheck --shell sh myscript.shExactly. The title is just plain wrong. Furthermore, TFA does not argue a "case", it simply tries to specify the language.