Save command output in string variable keeping newlines

I have a script that transforms a file's data, in order to work more efficiently I want to alter the data in memory and then dump it to the file. Supose I want to modify a file that contains this:

> This is a line
> this is other line

I use a sed command to replace the '>' symbols with '#' ones:

transform_output=$(eval "sed ${sed_args[@]} $file" 2>&1)
echo -e $transform_output

I get the output:

# This is a line # this is other line

rather than the output I would like to have wich is:

# This is a line
# this is other line
  1. How do I save the output in the string variable keeping the newlines?
  2. How could I add a line at the begining of the variable that contains "#ADDED LINE#" and save that in a file?

(the file I want to obtain would be):

#ADDED LINE#
# This is a line
# this is other line

Thanks in advance

Answers


  1. Quoting is used to prevent word-splitting at whitespace:

    echo -e "$transform_output"

  2. Group the command with another echo:

    { echo "#ADDED LINE#"; echo -e "$transform_output" } > file


Need Your Help

Using “action” in Twitter Boostrap classes

php html css twitter-bootstrap

I'm a newbie programmer trying to utilize Twitter Bootstrap to build out a concept. I'm using PHP and assigning actions within HTML tags to POST data and essentially take a user through the naviga...

Pass data from class to Form through different class

c# pass-data

Is there a better solution to send data from one class to another through different class? I have image in class ImageProccessing and I would like to send him to class MainForm.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.