In bash (and many other shells), $() is used to take the output of a command, and use it as the arguments of another command.
Here, what it does is :
First execute cat :
1
2
3
Then substitute the $() block with this :
echo 1
2
3
Now the shell needs to interpret this input, and send correct arguments to echo. To do that it must split 1, 2 and 3 in words : This is where the newlines are removed.
Finally, the shell sends the following :
Program : echo
Arguments : 1 2 3
This will display what you saw :
1 2 3
Because when echo has several arguments, it displays all of them separated with space.
removed during word splitting– Michael Homer Mar 06 '16 at 08:33echo $(cat myfile), does the bash manual say why the words are separated by white spaces, instead of not being seperated? – Tim Mar 06 '16 at 08:48$(...)in double-quotes. The entire output of the command-substitution will be treated as a single string. e.g.echo "$(cat myfile)"– cas Mar 07 '16 at 03:39