bug-coreutils
[Top][All Lists]
Advanced

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

Re: weird echo behaviour...


From: Andreas Schwab
Subject: Re: weird echo behaviour...
Date: Thu, 16 Sep 2004 18:15:11 +0200
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

"Alfred M. Szmidt" <address@hidden> writes:

> Could someone explain the following behaviour for me?  Because I sure
> do not understand it.

This has nothing to do with echo and everything to do with the shell.

> address@hidden:/tmp/foo$ touch 1 2 3 4 5
> address@hidden:/tmp/foo$ foo=`ls`

The value of foo now contains a bunch of newlines, among others.

> address@hidden:/tmp/foo$ /bin/echo $foo
> 1 2 3 4 5

Unquoted variable substitution splits on whitespace and /bin/echo receives
5 arguments, each of which is echoed separated by a single space.  This is
equivalent to:

$ /bin/echo 1 2 3 4 5

> address@hidden:/tmp/foo$ /bin/echo "$foo"
> 1
> 2
> 3
> 4
> 5

Quoted variable substitution does not split and preserves all whitespace
as is.  The (single) argument is echoed unchanged.  This is equivalent to:

$ /bin/echo "1
2
3
4
5"

> address@hidden:/tmp/foo$ foo='1 2 3 4 5'

Now foo contains a completely different value.

> address@hidden:/tmp/foo$ /bin/echo $foo
> 1 2 3 4 5

Still splitting on whitespace, still passing 5 arguments, same as first
example above.

> address@hidden:/tmp/foo$ /bin/echo "$foo"
> 1 2 3 4 5

Still no splitting, still passing one argument.  This is equivalent to:

$ /bin/echo "1 2 3 4 5"

Andreas.

-- 
Andreas Schwab, SuSE Labs, address@hidden
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




reply via email to

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