Reading file into variable, and losing line feeds

Nils Kassube kassube at gmx.net
Sat Nov 28 07:41:53 UTC 2009


Ray Parrish wrote:
> I am interested in being able to read the contents of a text file
>  into a variable, complete with line feeds.

> ray at RaysComputer:~/Bashscripts$ FileContents=`cat test.txt`
> ray at RaysComputer:~/Bashscripts$ echo $FileContents
> Line 1 Line 2 Line 3 Line 4 Line 5

> Is there some trick to preserving the line feeds in the variable?

The linefeeds are preserved but echo doesn't show them without quotes. 
Try this:

echo "$FileContents"


Nils




More information about the ubuntu-users mailing list