3 ms·
I used to think that way, but POSIX is not that well specified and there are tons of little behavior changes that makes the task difficult past 10 lines of code
by zimbatm 6y ago
I used to think that way, but POSIX is not that well specified and there are tons of little behavior changes that makes the task difficult past 10 lines of code. Writing portable shell scripts is like writing portable JavaScript in the IE days.
https://github.com/dylanaraps/pure-sh-bible https://github.com/dylanaraps/pure-sh-bible is a good example if you look at the issue tracker.
Once you accept Bash as a standard things become much easier. Basically Bash arrays, the `@Q` notation and ShellCheck combined solve all of the escaping issues that plague shell scripts.
- michaelcampbell 6y agoI can't think of a system that would be using anything I write that wouldn't have bash, honestly. Not in... 25 years or so. "portability" I guess is a goal, to a degree, for me too since I write in bash instead of zsh which I use interactively, but /bin/sh scripts for "portability" is just a fetish, IMO.
- apple4ever 6y agoYa bash is at least installed on everything modern now. Maybe that changes, but if you right for bash that's portable enough - you need to balance the work required with the risks of not doing it.
- joombaga 6y agoWhat is @Q notation?
- cb321 6y agoI belive zimbatm refers to the bash-specific ${var@Q} to expand to a form suitable as re-input into the shell. There are a few other bash-specific syntaxes like that (under "parameter transformation" in the manual page).
- zimbatm 6y agoWhat cb321 said. Sometimes programs take a single argument that contains the whole command. @Q allows to safely escape the list of arguments. This is a synthetic example: args=("my" "arg" "with space") bash -c "rm ${args[*]@Q}" This makes sure that the script will `rm` "my" "arg" and "with space", and not "with" and "space" :)
- graton 6y agoI found this info: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html https://www.gnu.org/software/bash/manual/html_node/Shell-Par... ${parameter@operator} The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter: Q The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
- lukeschlather 6y agoBash and shellcheck mitigate the issues but Powershell (and some newer shells like Elvish and Oil) actually solve the issues by introducing proper objects and breaking the defaults bash expects. Bash suffers from a similar problem to Perl and other 20+ year old "get it done" languages that they need a strict-mode bolted on, and a bolted-on strict mode is strictly inferior to just having the language behave sensibly to begin with.
- zimbatm 6y ago100% agreed, if you can choose your own language, there are certainly better options than Bash. It depends if you can do that or not. In some contexts, Python is also a pretty good option as it is available in a lot of places as well.
- turminal 6y ago> Once you accept Bash as a standard things become much easier. They also become nonstandard.
- zimbatm 6y agoThat's how I used to think as well. But you can also see Bash of being a standard on its own :) Obviously it all depends on the context.