Mutable Ideas

Notes and ideas about Java, Scala, Big Data, NoSQL, Quality and Software Deploy

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.

1
2
3
4
$ FILE=$(cat long-file.txt)

$ echo $FILE
line 1 line 2 line 3

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

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

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

1
2
$ echo "$FILE" | grep 2
line 2