There's a long history to this, which starts with the fact that adding any option support at all to the original echo breaks stuff. The result over the years has been quite a mess. The current Single Unix Specification has an note stating outright that one should simply not use -n or escape sequences at all with echo if one desires portability, and also stating that one should use printf instead.
* https://unix.stackexchange.com/q/65803/5132
* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/e...
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.
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
blackwhite=$(tput rev; echo -n " "; tput sgr0; echo -n " ")
whiteblack=$(tput sgr0; echo -n " "; tput rev; echo -n " ")
row_odd=$(for col in {1..4}; do echo -n "$whiteblack"; done; echo)
row_even=$(for col in {1..4}; do echo -n "$blackwhite"; done; echo)
chessboard=$(for i in {1..4}; do echo "$row_odd"; echo "$row_even"; done;tput sgr0)
For example, a slow version of printing a blank chess board:
And a faster version: In many cases, tput is doing doing anything more than looking up a string and printing it. Though in other cases like "tput cols", it is doing more. (And anyway, the number of columns isn't a constant.)