From owner-freebsd-questions@FreeBSD.ORG Mon Dec 1 23:50:58 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 740ECB70 for ; Mon, 1 Dec 2014 23:50:58 +0000 (UTC) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 377FEBCB for ; Mon, 1 Dec 2014 23:50:57 +0000 (UTC) Received: from r56.edvax.de (port-92-195-18-117.dynamic.qsc.de [92.195.18.117]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx02.qsc.de (Postfix) with ESMTPS id 10D242763B; Tue, 2 Dec 2014 00:50:55 +0100 (CET) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id sB1NoseA003116; Tue, 2 Dec 2014 00:50:55 +0100 (CET) (envelope-from freebsd@edvax.de) Date: Tue, 2 Dec 2014 00:50:54 +0100 From: Polytropon To: Yuri 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> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Questions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Dec 2014 23:50:58 -0000 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, ...