This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: About ENV?


John,

Please read the man section on variable substitution again.
The command
	$ AAA=aaa echo $AAA
sets the environment variable AAA to the value "aaa" **for the duration of
the echo command only**.  After the echo command is done, the environment
returns to its original state.  As the echo command does not access the
value of AAA in **its** environment, the effects of this assignment are
not discernible, but it does happen.  Compare with
	$ AAA=aaa sh -c 'echo $AAA'
where the same happens (i.e., AAA does not have the value "aaa" after the
command), but you can actually see the effects of the assignment.

If you're still unsure, go to the most up-to-date manual of all -- bash
source code.  :-)

I would also like to note that this whole discussion is rather off-topic
for this mailing list, as this is general bash behavior, rather than
cygwin-specific.
	Igor

On Fri, 25 Oct 2002, John Vincent wrote:

> Hi Igor,
>
> I agree with what you say except for one point: the command
>
> AAA=aaa echo $AAA
>
> does **NOT** set the shell variable AAA. Not before the echo
> command, not after the echo command. The shell variable is
> unchanged by this command. Only the environment variable
> AAA in the environment of the echo is changed. The echo is
> passed as argument the value of the AAA **shell** variable,
> not the AAA **environment** variable. (And as you say, this
> is expanded by the shell)
>
> Confusion in terminology sometimes arises because whenever
> a new shell starts, it initialises it's shell variables
> with the values it inherits from it's environment, people
> often forget that these two sets of variables are really
> quite independent, even though the shell deliberately tries
> to blur the destinction.
>
> I hope we're all clear now.
>
> /John Vincent.
>
> >From: Igor Pechtchanski <pechtcha@cs.nyu.edu>
> >Reply-To: cygwin@cygwin.com
> >To: John Vincent <jpv50@hotmail.com>
> >CC: cygwin@cygwin.com
> >Subject: Re: About ENV?
> >Date: Thu, 24 Oct 2002 14:13:55 -0400 (EDT)
> >
> >John,
> >
> >I fully understand your example.  And I think it's you who is confused.
> >
> >The example I provided was intended to illustrate that even though the
> >environment variable AAA *is* set to "aaa", the echo command *never sees*
> >that new value, as the sequence '$AAA' is expanded by the *originating
> >shell*, rather than the echo command itself.  In fact, the echo command is
> >not even aware it's echoing the value of some environment variable -- all
> >it sees is the value.  The parts of the example with the single quotes and
> >invoking another shell illustrated that once you protect the '$' from the
> >originating shell, the new shell *does* pick up the new value of the
> >environment variable.
> >
> >In other words, if you invoke a shell script that internally uses the
> >value of AAA, it will see the new value.  However, all the echo command
> >sees is the *value* of AAA, expanded for it by the originating shell,
> >which expansion happened *before* the assignment was in effect.
> >
> >A careful reading of the bash man page, particularly the section on
> >variable substitution, might be helpful here.
> >	Igor
> >
> >On Thu, 24 Oct 2002, John Vincent wrote:
> >
> > > Hi,
> > >
> > > I think you are confusing shell variables with environment
> > > variables. This is easy to do if you are not careful, since
> > > shell variables can be put into the environment using the
> > > "export" built-in command.
> > >
> > > Still, they are different things, and in this case the assignment
> > > is to an environment variable, not to a shell variable. It's
> > > done to keep backwards-compatibility with old shell scripts.
> > >
> > > The command:
> > >
> > > AAA=aaa echo $AAA
> > >
> > > does NOT change the shell variable AAA it changes the environment
> > > variable AAA for the echo command only. Maybe this example will
> > > make it clearer:
> > >
> > > Administrator@JPV ~
> > > $ AAA=bbb
> > >
> > > Administrator@JPV ~
> > > $ AAA=aaa echo $AAA
> > > bbb
> > >
> > > Administrator@JPV ~
> > > $
> > >
> > > In the above example the echo command is run with an environment
> > > variable AAA with a value of "aaa" but the shell variable AAA is
> > > passed with a value of "bbb" set on the previous line.
> > >
> > > I hope this helps.
> > >
> > > /John Vincent.
> > >
> > >
> > > >From: Igor Pechtchanski <pechtcha@cs.nyu.edu>
> > > >Reply-To: cygwin@cygwin.com
> > > >To: Andrew DeFaria <Andrew@defaria.com>
> > > >CC: cygwin@cygwin.com
> > > >Subject: Re: About ENV?
> > > >Date: Fri, 18 Oct 2002 11:36:49 -0400 (EDT)
> > > >
> > > >On Fri, 18 Oct 2002, Andrew DeFaria wrote:
> > > >
> > > > > John Vincent wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Not quite right there ... You can preceed a command with an
> > > > > > assignment, and the assigned variable is then put into the
> > > > > > environment of the command that is run. Thus the command
> > > > > > "echo" is run with an environment variable AAAA with the value
> > > > > > aaaa.  However, this does not change the AAAA shell variable
> > > > > > (which is currently empty) so the echo prints a blank (it's
> > > > > > empty argument)  and ignores the AAAA in it's environment.
> > > > > >
> > > > > > This syntax is useful for setting environment variables for one
> > > > > > command invocation only. They are not saved.
> > > > > >
> > > > > > I hope this fully explains what is going on.
> > > > >
> > > > > It doesn't. If "This syntax is useful for setting environment
> > > > > variables for one command invocation only" then the echo command
> > > > > should have echoed "aaa". Otherwise the syntax is not useful for
> > > > > setting environment variables for one command invocation.
> > > > > Perhaps what is meant is that this syntax is useful for
> > > > > overriding environment variables for one command invocation?
> > > > > However this leaves a glaring inconsistancy prone to error if
> > > > > the variable was not set already then no override takes place.
> > > >
> > > >Andrew,
> > > >
> > > >[pechtcha:~] AAAA=aaa && echo $AAAA
> > > >aaa
> > > >[pechtcha:~] AAAA=aaa echo $AAAA
> > > >
> > > >[pechtcha:~] AAAA=aaa eval 'echo $AAAA'
> > > >aaa
> > > >[pechtcha:~] AAAA=aaa sh -c 'echo $AAAA'
> > > >aaa
> > > >[pechtcha:~]
> > > >
> > > >Does this help?
> > > >       Igor

-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Water molecules expand as they grow warmer" (C) Popular Science, Oct'02, p.51


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]