backticks zapping whitespace - arrgh

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 ("A<space><space><space>A"). Three
whitespaces.

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

then I get this "A<space>A"

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)
usenet [ Do, 08 November 2007 04:22 ] [ ID #1865315 ]

Re: backticks zapping whitespace - arrgh

usenet [at] 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 ("A<space><space><space>A"). Three
> whitespaces.
>
> But if I do this:
> foo=`echo "A___A" |tr '_' ' ' ` ; echo $foo

....; echo "$foo"

> then I get this "A<space>A"
>
> 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.
Ed Morton [ Do, 08 November 2007 04:34 ] [ ID #1865318 ]

Re: backticks zapping whitespace - arrgh

On Nov 7, 7:34 pm, Ed Morton <mor... [at] lsupcaemnt.com> 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)
usenet [ Do, 08 November 2007 06:18 ] [ ID #1865319 ]

Re: backticks zapping whitespace - arrgh

usenet [at] DavidFilmer.com wrote:
>
> On Nov 7, 7:34 pm, Ed Morton <mor... [at] lsupcaemnt.com> 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
krahnj [ Do, 08 November 2007 07:15 ] [ ID #1865320 ]
Linux » comp.unix.shell » backticks zapping whitespace - arrgh

Vorheriges Thema: ${${var}}
Nächstes Thema: fg,bg functionality