Shell question: how to preserve newlines when process output is assigned to variable?

Polytropon freebsd at edvax.de
Mon Dec 1 23:50:58 UTC 2014


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, ...


More information about the freebsd-questions mailing list