[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Handle each file at a time in loop?

In <3f8297b21001041230y69f14fe2m653f899feb45cbdd@mail.gmail.com>, Foss User 
>$ ls
>convert.sh   Track 1.wav  Track 3.wav  Track 5.wav  Track 7.wav  Track 9.wav
>Track 1.mp3  Track 2.wav  Track 4.wav  Track 6.wav  Track 8.wav
>So, you can see there are file names with spaces in them. I have
>written a script like this to handle one file name at a time.
>for file in `ls *.wav`
>    echo $file
>Of course, this doesn't do what I need. The names are split wherever
>there are spaces.

That's because you are using a useless subshell for a useless ls, and failing 
to quote your variables.  You need:

for f in *.wav; do
	echo "$file"

ls is not responsible for doing filename expansion ("globbing"); the shell is, 
so let the shell do it.  Since you aren't using a subshell, word-splitting is 
not done on its output.

The quotes are required to prevent the shell from performing word-splitting on 
the contents of the variable; i.e. to prevent the filename "Track  2.wav" to 
be sent to the echo command/built-in as two arguments: "Track" and "2.wav".  
You wouldn't notice a difference in this case unless your filenames contained 
tabs, newlines, or consecutive whitespace characters.
Boyd Stephen Smith Jr.                   ,= ,-_-. =.
bss@iguanasuicide.net                   ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy         `-'(. .)`-'
http://iguanasuicide.net/                    \_/

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply to: