For loops, Spaces in values
Darryl Ross
bofh at afoyi.com
Tue Nov 28 00:46:26 CST 2006
shaun etherton wrote:
> Andrew Reid wrote:
>> Suppose I have a program that returns a list of files, but those
>> files contain spaces:
>>
>> /tmp/My File One.txt
>> /tmp/My File Two.txt
>>
>> If I try and do a for loop over it, like:
>>
>> for i in `listdirs` do
>> echo $i
>> done
>>
>> (where list dirs is the program that generates the aforementioned
>> output), I get the following output from the loop:
>>
>> My
>> File
>> One.txt
>> My
>> File
>> Two.txt
>>
>> So, how do I get the for loop to iterate over my list, but not
>> delimit that list by spaces?
>>
>> Cheers,
>>
>> - andrew
>>
>
> I think you need to change the delimeter from a space to a newline
> character. I can't remember the name of the variable though. :)
>
> IFS I think..
Yes, it is $IFS:
darryl at darryl-laptop:~/tmp$ ls -l
total 12
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test1
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test 2
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test 3
darryl at darryl-laptop:~/tmp$ for s in *; do echo "---->" $s; ls -dl $s; done
----> test1
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test1
----> test 2
ls: test: No such file or directory
ls: 2: No such file or directory
----> test 3
ls: test: No such file or directory
ls: 3: No such file or directory
darryl at darryl-laptop:~/tmp$ export IFS=$'\n' && for s in *; do echo
"---->" $s; ls -dl $s; done
----> test1
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test1
----> test 2
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test 2
----> test 3
drwxr-xr-x 2 darryl darryl 4096 2006-11-28 10:45 test 3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://www.linuxsa.org.au/pipermail/linuxsa/attachments/20061127/6110ad02/signature.bin
More information about the linuxsa
mailing list