[ale] Keep space separated values sane in a bash "for" loop?

Christopher Bergeron christopher at bergeron.com
Fri Apr 6 01:38:34 EDT 2007


Drat!  Something isn't working.  Perhaps its the fact that I'm beyond my 
bedtime, or perhaps this code just doesn't work:

#!/bin/bash
export oldifs=$IFS
export IFS=" $'\n' "
for i in `\ls`; do echo $i; done
export IFS=$oldifs


The code works, but it interprets every "n" literally and substitutes it 
with a newline.  I'm missing something obvious here, but it's late, and 
I'm tired.  Perhaps someone can spot it.  The $IFS var, uses a $ and 
regexps within single quotes - so what am I missing?

Kind regards and TIAa,
CB




James P. Kinney III wrote:
> The issue is the $IFS character is set by the default bash environment.
> It is always " ". To muck with it do the following:
>
> oldifs=$IFS
>
> IFS=<new character to split on>
>
> Here's where is gets wacky. Most likely what you really want to do is
> split the stuff on the new line character. In bash it is "\n".
>
> BUT things can get wonky depending on more stuff than I can remember.
>
> But this works:
>
> export oldifs=$IFS
> export IFS="\n"
> for i in `ls -1`; do echo $i; done
> file 3
> file1
> file2
>
> Close the shell and it all goes away. :)
>
> On Thu, 2007-04-05 at 20:37 -0400, Christopher Bergeron wrote:
>   
>> J.D.  - double quoting the output doesn't add any benefit when used in a 
>> bash for loop.  For example:
>>
>> for i in `\ls -Q`; do echo "Filename: $i:"; done
>>
>> Still breaks the filenames up at the space.  I've tried escaping the 
>> quotes (single and double), so I don't think that's the correct 
>> solution.  I appreciate your suggestion though, so thank you.
>>
>> Kind regards,
>> CB
>>
>>
>> J. D. wrote:
>>     
>>> If you are using ls you could try looking into the Q option. It 
>>> double-quotes
>>> the output.
>>>
>>> ls -Q
>>>
>>> Best regards,
>>>
>>> J. D.
>>>
>>> On 4/5/07, *Christopher Bergeron * <christopher at bergeron.com 
>>> <mailto:christopher at bergeron.com>> wrote:
>>>
>>>     Guys, I'm not sure how to word my question appropriately, so I'll
>>>     try to
>>>     explain what I'm trying to do in code.
>>>
>>>     I have a bash for loop, and when it encounters a value with a
>>>     space, it
>>>     splits it into separate entities.  For example:
>>>
>>>     With a file list of:
>>>     /bin
>>>     /usr
>>>     /Virtual Machines
>>>
>>>     and a code snippet of:
>>>
>>>     for i in *; do
>>>     echo "Filename is: $i"
>>>     done
>>>
>>>     The output is:
>>>
>>>     Filename is: bin
>>>     Filename is: usr
>>>     Filename is: Virtual
>>>     Filename is: Machines
>>>
>>>     How can I tell bash to keep these values together, so that I get the
>>>     filenames intact as:
>>>     Filename is: bin
>>>     Filename is: usr
>>>     Filename is: Virtual Machines
>>>
>>>     Thanks in advance...
>>>
>>>     Kind regards,
>>>     Chris Bergeron
>>>     http://chrisbergeron.com
>>>
>>>
>>>     _______________________________________________
>>>     Ale mailing list
>>>     Ale at ale.org <mailto:Ale at ale.org>
>>>     http://www.ale.org/mailman/listinfo/ale
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> _______________________________________________
>>> Ale mailing list
>>> Ale at ale.org
>>> http://www.ale.org/mailman/listinfo/ale
>>>       
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale
>>     
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list