2

From Bash manual about the result of command expansion

Embedded newlines are not deleted, but they may be removed during word splitting.

For example

$ cat myfile
1
2
3

$ echo $(cat myfile)
1 2 3

I wonder why embedded newlines are replaced with whitespaces? The manual doesn't say so, does it?

Tim
  • 101,790

2 Answers2

4

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.

techraf
  • 5,941
bew
  • 246
2

bew78 has answered the question, but just to make it more explicit --

Starting with command substitution in the source code, it calls word_list_split(), which itself then calls word_split(), which then calls list_string(), which specifically skips over IFS separators. That creates separate "words" that the list_string() function then returns up the stack. That's how 1, 2, and 3 end up as separate parameters to the echo command.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255