4 ms·
Thanks. With "echo", I am probably stuck in the past. I recall encountering systems where "printf" (the command) didn't exist. Maybe this is no longer a concern
by adrianmonk 6y ago
Thanks. With "echo", I am probably stuck in the past. I recall encountering systems where "printf" (the command) didn't exist. Maybe this is no longer a concern.
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:
$ for i in $(echo a b c)
> do
> echo "$i"
> done
a
b
c
But then, empirically, when assigning to a variable, anything I throw at it seems to come through unscathed even without quotes:
$ x=$(echo " * " b c"'"; echo d;)
$ echo "$x"
* b c'
d
$
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.
- JdeBP 6y agoIt'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://mywiki.wooledge.org/Quotes * https://unix.stackexchange.com/q/97560/5132 https://unix.stackexchange.com/q/97560/5132 * https://unix.stackexchange.com/q/68694/5132 https://unix.stackexchange.com/q/68694/5132 * https://unix.stackexchange.com/q/131766/5132 https://unix.stackexchange.com/q/131766/5132