From owner-freebsd-questions@FreeBSD.ORG Sat Aug 23 14:10:57 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 851331065673 for ; Sat, 23 Aug 2008 14:10:57 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) by mx1.freebsd.org (Postfix) with ESMTP id 4264D8FC19 for ; Sat, 23 Aug 2008 14:10:57 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r55.edvax.de (port-92-195-101-165.dynamic.qsc.de [92.195.101.165]) by mx02.qsc.de (Postfix) with ESMTP id 43C3216C03E6; Sat, 23 Aug 2008 16:09:59 +0200 (CEST) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id m7NE9w2r050163; Sat, 23 Aug 2008 16:09:58 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Sat, 23 Aug 2008 16:09:57 +0200 From: Polytropon To: David Banning Message-Id: <20080823160957.22e6254f.freebsd@edvax.de> In-Reply-To: <20080823101941.GA42601@skytracker.ca> References: <20080823101941.GA42601@skytracker.ca> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: questions@freebsd.org Subject: Re: space char shell script problem X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Aug 2008 14:10:57 -0000 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. :-) 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. 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, ...