How to store and echo multiple lines elegantly in bash?

Posted by EmpireJones on Stack Overflow See other posts from Stack Overflow or by EmpireJones
Published on 2010-05-02T06:28:50Z Indexed on 2010/05/02 6:37 UTC
Read the original article Hit count: 201

Filed under:
|
|

I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.

However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.

Any ideas regarding how I can accomplish this?

Example:

#!/bin/bash

read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK

echo $my_var

Output:

this is a test

Desired output:

this

is

a

test

© Stack Overflow or respective owner

Related posts about bash

Related posts about linux