why "" in echo "$FOO" ?

why "" in echo "$FOO" ?

Post by KKramsc » Mon, 29 Nov 2004 15:24:44



I just read in a book that to one way to find out the value of an
enviroment variable is to give it as the argument to echo, preceded
by a $ and surrounded by double quotes, as in echo "$FOO".  Why
the quotes?  It seems that I get the same results whether I do echo
$FOO or echo "$FOO".

Thanks!

        Karl

--
Sent from a spam-bucket account; I check it once in a blue moon.  If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.

 
 
 

why "" in echo "$FOO" ?

Post by William Par » Mon, 29 Nov 2004 15:31:55



> I just read in a book that to one way to find out the value of an
> enviroment variable is to give it as the argument to echo, preceded
> by a $ and surrounded by double quotes, as in echo "$FOO".  Why
> the quotes?  It seems that I get the same results whether I do echo
> $FOO or echo "$FOO".

> Thanks!

>         Karl

Hint:
    a='1  2'
    echo $a
    echo "$a"

--

Linux solution for data processing.

 
 
 

why "" in echo "$FOO" ?

Post by rakesh shar » Mon, 29 Nov 2004 21:38:52



> I just read in a book that to one way to find out the value of an
> enviroment variable is to give it as the argument to echo, preceded
> by a $ and surrounded by double quotes, as in echo "$FOO".  Why
> the quotes?  It seems that I get the same results whether I do echo
> $FOO or echo "$FOO".

To avoid mangling the space/TABs/newlines in the variable name

AND

to avoid wildcard expansion incase FOO contains the file-globbing
chars like * ? [ etc.

 
 
 

why "" in echo "$FOO" ?

Post by Stephane CHAZELA » Mon, 29 Nov 2004 22:15:04


2004-11-28, 06:24(+00), KKramsch:
Quote:> I just read in a book that to one way to find out the value of an
> enviroment variable is to give it as the argument to echo, preceded
> by a $ and surrounded by double quotes, as in echo "$FOO".  Why
> the quotes?  It seems that I get the same results whether I do echo
> $FOO or echo "$FOO".

[...]

That's because of the special behavior of Bourne like shells
(not zsh unless in sh/ksh compatibility mode). When a variable
is left unquoted, word splitting occurs and filename generation
is done.

That means that if

var="1|*"

(and the internal field separator is "|" (IFS="|"))

in:

echo $var

echo is passed several arguments: "1" and the list of (non-dot)
filenames in the current directory.

zsh (and rc and es) have a more consistent behavior, however
note that (not for rc nore es that have a yet more consistent
behavior)

echo $var

and

echo "$var"

are different in case $var is empty. In the first case, echo is
passed no argument. In the second, it is passed one empty one.

--
Stephane

 
 
 

why "" in echo "$FOO" ?

Post by KKramsc » Tue, 30 Nov 2004 00:37:25


Thanks!

        Karl
--
Sent from a spam-bucket account; I check it once in a blue moon.  If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.