From owner-freebsd-questions@FreeBSD.ORG Sat Aug 23 14:27:10 2008 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 10B671065672 for ; Sat, 23 Aug 2008 14:27:10 +0000 (UTC) (envelope-from dickey@saltmine.radix.net) Received: from saltmine.radix.net (saltmine.radix.net [207.192.128.40]) by mx1.freebsd.org (Postfix) with ESMTP id A87C58FC1B for ; Sat, 23 Aug 2008 14:27:09 +0000 (UTC) (envelope-from dickey@saltmine.radix.net) Received: from saltmine.radix.net (localhost [127.0.0.1]) by saltmine.radix.net (8.12.2/8.12.2) with ESMTP id m7NEGah6002461 for ; Sat, 23 Aug 2008 10:16:37 -0400 (EDT) Received: (from dickey@localhost) by saltmine.radix.net (8.12.2/8.12.2/Submit) id m7NEGasj002452 for questions@freebsd.org; Sat, 23 Aug 2008 10:16:36 -0400 (EDT) Date: Sat, 23 Aug 2008 10:16:36 -0400 From: Thomas Dickey To: questions@freebsd.org Message-ID: <20080823141636.GA439@saltmine.radix.net> References: <20080823101941.GA42601@skytracker.ca> <20080823160957.22e6254f.freebsd@edvax.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20080823160957.22e6254f.freebsd@edvax.de> User-Agent: Mutt/1.3.27i Cc: Subject: Re: space char shell script problem 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: Sat, 23 Aug 2008 14:27:10 -0000 On Sat, Aug 23, 2008 at 04:09:57PM +0200, Polytropon wrote: > On Sat, 23 Aug 2008 06:19:42 -0400, David Banning wrote: > > I am running into a problem with the space character in filenames. > > For instance, If I want to run the script; > > > > for x in `ls` > > do > > echo $x > > done > > > > then filenames that have a space in them ie: "john smith.jpg" > > are processed by my script as two names, "john" and "smith.jpg". > > > > What is the best way to deal with this type of space problem in the shell? > > The best way is not to use spaces in filenames; underscores perform > their purpose very well without making things more complicated. :-) spaces won't go away, and since they're legal in filenames, one may as well handle them. > To iterate over files, I would not use `ls`, instead, I would let > the shell do the expansion of * for me, as it has already been > suggested. > > Because the file names x iterates contain spaces, be very (!) careful > to assure that the applications you call with these filenames get > the spaces correctly masked, either put the filename in quotes or > substituts " " by "\ ". You would have won nothing when the application > you call with the filename interpretes it as an argument list with > two elements. A script like #!/bin/sh for x in "$@" do echo $x done handles quoting nicely enough (for spaces, anyway). ls will translate some non-printing characters to printable; the 'find' program is a better alternative if one must derive the list inside the program. > for x in *; do > echo "${x}" > done > > Replace the middle line with any program call you want. > > > -- > Polytropon > >From Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Thomas E. Dickey http://invisible-island.net ftp://invisible-island.net