Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 23 Aug 2008 16:09:57 +0200
From:      Polytropon <freebsd@edvax.de>
To:        David Banning <david+dated+1219918782.39167f@skytracker.ca>
Cc:        questions@freebsd.org
Subject:   Re: space char shell script problem
Message-ID:  <20080823160957.22e6254f.freebsd@edvax.de>
In-Reply-To: <20080823101941.GA42601@skytracker.ca>
References:  <20080823101941.GA42601@skytracker.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 23 Aug 2008 06:19:42 -0400, David Banning <david+dated+1219918782.39167f@skytracker.ca> 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, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080823160957.22e6254f.freebsd>