I tend to quote command substitutions as shown below even when assigning their output to a variable:
Is that actually needed though? When does it break? The accepted answer here claims:
DIRNAME="$(dirname $FILE)" will not do what you want if $FILE contains whitespace or globbing characters [?*.
The link points to Grey Cat Wiki's great page about quoting but that page doesn't mention quoting command substitutions specifically. And while quoting the variable is clearly needed, quoting the command substitution itself doesn't seem to be.
However, the same post concludes with:
DIRNAME="$(dirname "$FILE")" is the recommended way. You can replace DIRNAME= with a command and a space without changing anything else, and dirname receives the correct string.
Which is what I've always thought as well and have often ocrrected posts here that didn't quote it. However, the wiki page linked to above also claims that:
There are a few cases where double quotes may be safely omitted:
On the right hand side of a simple assignment. You may write foo=$bar without quotes. This is POSIX compliant.
[. . . ]
var=$(command) isn't really a "simple" assignment, I was nevertheless unable to find a case where the quotes were actually necessary:
$ var=$(echo "foo bar baz") ## whitespace works $ echo "$var" foo bar baz $ var=$(printf "foo\nbar * baz") ## so do globbing characters $ echo "$var" foo bar * baz $ var1="foo\nbar * baz" $ var=$(printf "$var1") ## printing a variable doesn't make any difference $ echo "$var" foo bar * baz $ var=$(printf '%s\n' "$var1") $ echo "$var" foo\nbar * baz $ var=$(printf -- '-e %s\n' "$var1") ## strings starting with - also work $ echo "$var" -e foo\nbar * baz
Of course, the quotes are absolutely necessary if the command substitution is being used directly for things like
command1 "$(command2)", but that doesn't seem to be the case when assigning to a variable.
So, what am I missing? Are the quotes ever needed? What corner case will quoting a command substitution when assigning its return value to a variable protect you from? Or is it always OK to not quote a command substitution if it is the right hand side of a variable assignment operation?