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

Ralf Mardorf ralf.mardorf at rocketmail.com
Mon Dec 1 23:41:34 UTC 2014


On Mon, 01 Dec 2014 15:22:24 -0800
Yuri <yuri at 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 at archlinux Desktop]$ ls
bar  foo
[rocketmouse at archlinux Desktop]$ VAR=$(ls) ; echo $VAR
bar foo
[rocketmouse at archlinux Desktop]$ VAR=$(ls) ; echo "$VAR"
bar
foo
[rocketmouse at 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 at 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 at 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


More information about the freebsd-questions mailing list