Foreword: I've already seen possibly related question Difference between 'ls' and 'echo $(ls)'
Does bash perform word splitting on output capturing? This guide -- mywiki.wooledge.org/WordSplitting says "no":
Word splitting is not performed on expansions in assignments. Thus, one does not need to quote anything in a command like these: [...]
bar=$(a command)
But when I do res=$(find); echo $res
output differs from just find
:
$ find
.
./file2
./file1
./test.sh
$ res="$(find)"; echo $res
. ./file2 ./file1 ./test.sh
$ res=$(find); echo $res
. ./file2 ./file1 ./test.sh
GNU bash, version 5.0.18(1)-release (x86_64-slackware-linux-gnu)
– Arkadiusz Drabczyk Sep 27 '20 at 20:23echo
. You may want to tryecho "$res"
. – Kusalananda Sep 27 '20 at 20:34bash
processes an unquoted variable reference (e.g.echo $res
), it'll word-split the result (assuming default options). zsh does not do this (again assuming default options). – Gordon Davisson Sep 27 '20 at 20:37res=$(find); echo $res
it differs from justfind
– aryndin Sep 27 '20 at 20:41res=$(find)
, but it does inecho $res
. See this question and its answer. Personally, I think it's easier to just double-quote all variable and command substitutions, rather than trying to keep track of where it's safe to omit the quotes and where it can cause trouble. – Gordon Davisson Sep 27 '20 at 22:48