bug-bash
[Top][All Lists]
Advanced

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

Re: for ... in ... do ignores escape characters


From: Dave Rutherford
Subject: Re: for ... in ... do ignores escape characters
Date: Fri, 18 Apr 2008 08:02:11 -0400

On Thu, Apr 17, 2008 at 4:21 PM, luiscolorado <luisc@fmssolutions.com> wrote:
>  Hello, everybody... I'm suffering a huge problem with the "for" command
>  because it wants to parse a variable ignoring escape characters.
>
>  For example, as you know, if I do something like this in bash:
>
>  for i in sony apple "hewlett packard"
>  do
>  echo $i
>  done
>
>  It will echo the following:
>
>  sony
>  apple
>  hewlett packard
>
>  However, I have a program that generates a list of files (let's say like the
>  previous one) that I want to use. This is what I get when some file names
>  have spaces:
>
>  for i in `my-program`
>  do
>  echo $i
>  done
>
>  It echoes the following:
>
>  sony
>  apple
>  hewlett
>  packard
>
>
>  Did you notice how it broke "hewlett packard" in two separate strings? That
>  is, it uses the spaces to parse the file names!

Quotes or escapes in the output of the `` are input to the shell rather
than shell syntax, so won't be interpreted.  You just need to quote more.

$ foo () { echo sony; echo apple; echo hewlett packard; }

Now note the difference between:

$ for w in "`foo`"; do echo $w; done
sony apple hewlett packard
   and
$ for w in `foo`; do echo "$w"; done
sony
apple
hewlett
packard
   and
$ for w in "`foo`"; do echo "$w"; done
sony
apple
hewlett packard

Why the first one does that I'm not sure, but it's the last one you want.

Dave




reply via email to

[Prev in Thread] Current Thread [Next in Thread]