Re:Pure sh bible – Posix sh alternatives to external processes
by JdeBP on Wednesday October 21, 2020 @07:22AM
(#24846681)
It's one of those things where the exact rules are complex and easy to misremember. Ironically, your two scripts demonstrate one way in which the problem arises. The rules are different for each script, because the command expansions are in different contexts. And when someone in the future decides to make your code into a shell function with local variables, suddenly the rules change again.
For maintainability, for safety, for the people who come after one, and for one's own sanity, the general advice is to always quote.
* https://mywiki.wooledge.org/Quotes
* https://unix.stackexchange.com/q/97560/5132
I'm less sure whether the quoting is technically necessary. It's probably better style to quote anyway, so this is mostly academic, but I'll ask anyway.
Does word splitting, etc. occur when assigning a command substitution to a variable? I know there are times when it does occur:
But then, empirically, when assigning to a variable, anything I throw at it seems to come through unscathed even without quotes: No wildcards were expanded, spaces and quotes weren't stripped, etc.I couldn't find in the standards doc (that you linked) anything that addresses this either way.