Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Dec 2014 00:41:29 +0100
From:      Ralf Mardorf <ralf.mardorf@rocketmail.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: [Bulk] Shell question: how to preserve newlines when process output is assigned to variable?
Message-ID:  <20141202004129.0c489bf3@archlinux>
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 <yuri@rawbw.com> 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?

For Linux bash it does work like this:

[rocketmouse@archlinux Desktop]$ ls
bar  foo
[rocketmouse@archlinux Desktop]$ VAR=$(ls) ; echo $VAR
bar foo
[rocketmouse@archlinux Desktop]$ VAR=$(ls) ; echo "$VAR"
bar
foo
[rocketmouse@archlinux Desktop]$ ls -hAl
total 0
-rw-r--r-- 1 rocketmouse rocketmouse 0 Dec  2 00:36 bar
-rw-r--r-- 1 rocketmouse rocketmouse 0 Dec  2 00:36 foo
[rocketmouse@archlinux Desktop]$ VAR=$(ls -hAl) ; echo $VAR
total 0 -rw-r--r-- 1 rocketmouse rocketmouse 0 Dec 2 00:36 bar -rw-r--r-- 1 rocketmouse rocketmouse 0 Dec 2 00:36 foo
[rocketmouse@archlinux Desktop]$ VAR=$(ls -hAl) ; echo "$VAR"
total 0
-rw-r--r-- 1 rocketmouse rocketmouse 0 Dec  2 00:36 bar
-rw-r--r-- 1 rocketmouse rocketmouse 0 Dec  2 00:36 foo



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