Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Dec 2014 00:50:54 +0100
From:      Polytropon <freebsd@edvax.de>
To:        Yuri <yuri@rawbw.com>
Cc:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Shell question: how to preserve newlines when process output is assigned to variable?
Message-ID:  <20141202005054.fd5729f2.freebsd@edvax.de>
In-Reply-To: <547CF830.5000907@rawbw.com>
References:  <547CF830.5000907@rawbw.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 01 Dec 2014 15:22:24 -0800, Yuri wrote:
> 
> When script has the line like this:
> VAR=$(ls)
> all newlines returned by the process (ls) are removed.
> 
> I know shell variables can hold newlines in them when assigned inside 
> the script. Just in this case, when the process output is assigned, they 
> are stripped.
> 
> Any way to keep newlines?

Yes: You simply have to use proper quoting:

	VAR=$(ls)
	echo "$VAR"

The newlines will now be displayed correctly.
Similarly, you use "$VAR" for further processing.


-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141202005054.fd5729f2.freebsd>