From owner-freebsd-questions@FreeBSD.ORG Fri Sep 28 21:09:53 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BACC416A41A for ; Fri, 28 Sep 2007 21:09:53 +0000 (UTC) (envelope-from wblock@wonkity.com) Received: from wonkity.com (wonkity.com [67.158.26.137]) by mx1.freebsd.org (Postfix) with ESMTP id 697DC13C480 for ; Fri, 28 Sep 2007 21:09:53 +0000 (UTC) (envelope-from wblock@wonkity.com) Received: from wonkity.com (localhost [127.0.0.1]) by wonkity.com (8.14.1/8.14.1) with ESMTP id l8SL9qAF029595; Fri, 28 Sep 2007 15:09:52 -0600 (MDT) (envelope-from wblock@wonkity.com) Received: from localhost (wblock@localhost) by wonkity.com (8.14.1/8.14.1/Submit) with ESMTP id l8SL9qcr029592; Fri, 28 Sep 2007 15:09:52 -0600 (MDT) (envelope-from wblock@wonkity.com) Date: Fri, 28 Sep 2007 15:09:52 -0600 (MDT) From: Warren Block To: jhall@vandaliamo.net In-Reply-To: <21079.67.171.53.31.1191004462.squirrel@admintool.trueband.net> Message-ID: <20070928140159.M29117@wonkity.com> References: <21079.67.171.53.31.1191004462.squirrel@admintool.trueband.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (wonkity.com [127.0.0.1]); Fri, 28 Sep 2007 15:09:52 -0600 (MDT) Cc: freebsd-questions@freebsd.org Subject: Re: Adding CR/LF X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Sep 2007 21:09:53 -0000 On Fri, 28 Sep 2007, jhall@vandaliamo.net wrote: > I know this should be easy, but I cannot get it to work right. Basically, > I have a list of items, and I need to place each one on a separate line. > > Here is the script I am using. > #!/bin/sh > FILENAMES="test1 test2 test3" > FILELIST="" > for filename in ${FILENAMES} > do > FILELIST="${FILELIST}${filename}"$'\n\r' > echo ${FILELIST} > done > > And, here is the output I am getting. > test1$\n\r > test1$\n\rtest2$\n\r > test1$\n\rtest2$\n\rtest3$\n\r > > The output I would like to see is: > test1 > test2 > test3 It took me a bit to realize that what you're trying to do is go from a variable with a space-separated list of filenames to a variable with a newline-separated list. If you don't really need that second variable but just want to show those names on the screen, just echo ${filename} in the loop. echo appends a linefeed. Or use printf, which can understand standard character escapes. If you really want a new variable, echo the FILENAMES variable into tr to replace spaces with newlines. (\r is not needed.) String manipulation in sh is painful at best. Any of the scripting languages are better at this. -Warren Block * Rapid City, South Dakota USA