echo removing new line

I would like to cache a result from a long computation in bash, so I set a variable with this value, for my surprise I got everything in one line, instead of the desired multi-line output.

This behavior of echo command drove me crazy for a while.

$ FILE=$(cat long-file.txt)

$ echo $FILE
line 1 line 2 line 3

The correct syntax is really subtle, involving double quotes "".

$ echo "$FILE"
line 1
line 2
line 3

Now I’m able to use pipe chain to the next commands:

$ echo "$FILE" | grep 2
line 2

Gustavo Arjones

Always Learning, Geek, Curious