[SLE] bash Script Using Filenames with Embedded Spaces



I am trying to write a simple bash script to handle files with embedded
spaces in the filename. For example, a listing of a directory using "ls -1"
gives:

ls -l *.mp3
-rw-r--r-- 1 lucky users 20168832 2006-11-03 17:54 01 Allegro.mp3
-rw-r--r-- 1 lucky users 8105984 2006-11-03 17:53 02 Adagio un poco mosso.mp3
-rw-r--r-- 1 lucky users 10661760 2006-11-03 17:53 03 Rondo - Allegro.mp3

The following simplified script

for i in `ls -1 *.mp3`
do
echo $i
done

produces the following output:

01
Allegro.mp3
02
Adagio
un
poco
mosso.mp3
03
Rondo
-
Allegro.mp3

I have tried various combinations of single and double quotes to no avail
including redirecting the "ls -1" listing to a file and escaping the
spaces with a backslash, again to no avail.

Surely there must be some trick I can use to handle this anomoly.?

Thank you,
Lucky Leavell

--
Check the headers for your unsubscription address
For additional commands send e-mail to suse-linux-e-help@xxxxxxxx
Also check the archives at http://lists.suse.com
Please read the FAQs: suse-linux-e-faq@xxxxxxxx



Relevant Pages

  • Re: [SLE] bash Script Using Filenames with Embedded Spaces
    ... On 11/6/06, Lucky Leavell wrote: ... spaces in the filename. ... a listing of a directory using "ls -1" ... The following simplified script ...
    (SuSE)
  • Re: checking files using wildcards
    ... [This followup was posted to comp.unix.shell] ... I made a slight omission on my previous post, ... > I'm writing a bash script that includes checking for non-desireable ... > The problem, as I've come to realize, is that $filename is checked ...
    (comp.unix.shell)
  • Re: Simple bash script to grep files for bad keywords
    ... >> I am trying to write a simple bash script that will grep all files ... > Use xargs, since it will buy you the extra feature of being able to ... > effectively forcing grepto print the filename of this one file ...
    (freebsd-questions)
  • Modifying ls
    ... I want to some how modify the contents of a ls listing to only display ... the filename and the time it was modified. ...
    (comp.lang.perl.misc)
  • Re: checking files using wildcards
    ... >I'm writing a bash script that includes checking for non-desireable ... the script prompts the user to remove ... >The problem, as I've come to realize, is that $filename is checked ...
    (comp.unix.shell)