On Sat, Oct 2, 2010 at 9:08 AM, Ryan Hill <[email protected]> wrote:
> On Fri, 1 Oct 2010 20:47:38 +0530
> Nirbheek Chauhan <[email protected]> wrote:
>>
>> A nice way around this is to do the following:
>>
>>     ls -1 | while read i; do
>>
>> `read` delimits on newline, so you're safe.
>
> This strips leading and trailing whitespace.
>
> $ touch " test1"
> $ touch "test2 "
> $ touch "test 3"
> $ touch " test 4 "
>
> $ ls -1 | while read i; do echo "###${i}###"; done
> ###test2###
> ###test 3###
> ###test1###
> ###test 4###
>

Damn, I never knew that.

> instead use:
>
> $ ls -1 | while IFS= read i; do echo "###${i}###"; done
> ###test2 ###
> ###test 3###
> ### test1###
> ### test 4 ###
>
> or recursively:
>
> $ find . -type f -print0 \
>  | while IFS= read -d $'\0' i; do echo "###${i}###"; done
> ###./ test1###
> ###./ test 4 ###
> ###./test 3###
> ###./test2 ###
>
> or just make things simple on yourself :)
>
> $ for i in *; do echo "###${i}###"; done
> ### test1###
> ###test2 ###
> ###test 3###
> ### test 4 ###
>

I wonder how many scripts I wrote now have this subtle bug. Thanks!

-- 
~Nirbheek Chauhan

Gentoo GNOME+Mozilla Team

Reply via email to