backticks zapping whitespace - arrgh

backticks zapping whitespace - arrgh

am 08.11.2007 04:22:48 von usenet

If I do this on AIX or Linux (trivial example to illustrate the core
of the problem):

echo "A___A" |tr '_' ' '

then I get what I expected to see ("AA"). Three
whitespaces.

But if I do this:
foo=`echo "A___A" |tr '_' ' ' ` ; echo $foo

then I get this "AA"

One whitespace.

The extra whitespaces are being compressed (as if I were doing tr -s,
which I am not, and I have no such alias).

How can I assign $foo and have it preserve multiple whitespace???

Thanks!

--
David Filmer (http://DavidFilmer.com)

Re: backticks zapping whitespace - arrgh

am 08.11.2007 04:34:52 von Ed Morton

usenet@DavidFilmer.com wrote:
> If I do this on AIX or Linux (trivial example to illustrate the core
> of the problem):
>
> echo "A___A" |tr '_' ' '
>
> then I get what I expected to see ("AA"). Three
> whitespaces.
>
> But if I do this:
> foo=`echo "A___A" |tr '_' ' ' ` ; echo $foo

....; echo "$foo"

> then I get this "AA"
>
> One whitespace.
>
> The extra whitespaces are being compressed (as if I were doing tr -s,
> which I am not, and I have no such alias).
>
> How can I assign $foo and have it preserve multiple whitespace???


Always quote your variables unless you have a very specific reason not to.

Ed.

Re: backticks zapping whitespace - arrgh

am 08.11.2007 06:18:52 von usenet

On Nov 7, 7:34 pm, Ed Morton wrote:
> ...; echo "$foo"

> Always quote your variables unless you have a very specific reason not to.

Thanks! That's taboo in Perl... and I was going nuts trying to figure
this out. It never ocured to me to quote the var!



--
David Filmer (http://DavidFilmer.com)

Re: backticks zapping whitespace - arrgh

am 08.11.2007 07:15:38 von krahnj

usenet@DavidFilmer.com wrote:
>
> On Nov 7, 7:34 pm, Ed Morton wrote:
> > ...; echo "$foo"
>
> > Always quote your variables unless you have a very specific reason not to.
>
> Thanks! That's taboo in Perl... and I was going nuts trying to figure
> this out. It never ocured to me to quote the var!

It is not taboo but unlike the shell it is not required. The FAQ
explains the issues:

perldoc -q quoting

Found in /usr/lib/perl5/5.6.0/pod/perlfaq4.pod
What's wrong with always quoting "$vars"?



John
--
use Perl;
program
fulfillment