[Novalug] question about variables ($...) (was: Re: question about ls)
James Ewing Cottrell 3rd
JECottrell3 at Comcast.NET
Wed Jan 18 23:04:17 EST 2012
After further review, the behavior listed in this post is confirmed.
Looking in the 4.2BSD manual, dated 7 February 1983, indeed confirms the
behavior listed below. Variables are Imported from the environment. The
environment remains oblivious to any changes made internally unless
those variables are re-exported. The man pages listed "7th Edition" at
the bottom of the pages, so this behavior comes from The Masters themselves.
Newer shells such as ksh and bash have the -x or 'export attribute',
which re-exports any changes to the environment. Page 168 of "The
KornShell" by Bolsky and Korn note that this is different behavior than
the Bourne Shell.
I'd say the newer behavior is more intuitive.
JIM
On 1/17/2012 5:55 PM, Jon LaBadie wrote:
> On Tue, Jan 17, 2012 at 06:49:11PM +0000, jecottrell3 at comcast.net wrote:
>> CSH based shells have two namespaces: exported variables created by 'setenv' and regular variables created by 'set'. You can get some 'interesting'* results by doing things like:
>>
>> set var=1
>> setenv var 2
>> echo $var
>> # result is 1
>> sh -c 'echo $var'
>> # result is 2 (from env)
>> unset var
>> echo $var
>> # result is 2
>>
>> Thus, the plain variable overrides the environment variable.
>>
>> This is yet another reason why CSH is abominable.
>>
>> Bourne shells are simpler. There is only one Namespace, and 'export' is a flag or attribute.
>> Variables are set by assignment, and the 'set' command sets the positional parameters, or, if there are no arguments, prints the variable list.
>>
>> JIM
>
> Way back when, say the mid-80's, there was some inconsistancy in the
> behavior of the Bourne shell with respect to environment variables.
> There did 'seem' to be two sets of variables, "local" and "environment".
> I was writing/maintaining AT&T's shell programming course at the time.
> The effect of changing a variable that was received from the environment
> seemed to flip with each new release of the OS and Bourne shell.
>
> In today's Bourne-style shells if a process changes the value of an
> variable received from the environment, i.e. exported by an earlier
> process, the new value is passed via the environment to its children.
>
> The odd behavior was for the new value to be used by the current
> process, but any children received the original environment value.
> To pass the new value to its children, the process had to re-export
> the variable. In some ways I felt this was reasonable behavior.
> The developer of the program could chose to pass on the original
> or their alternate value.
>
> Tracking these differences for the shell programming course became
> a real pain when we realized that even in the same release, the
> behavior varied among different computer architectures (vax, 3b2,
> 3b20). We had to maintain a table of behaviors showing releases
> and architecture.
>
>> ----- Original Message -----
>> From: "Walt Smith"<waltechmail at yahoo.com>
>> To: novalug at calypso.tux.org
>> Sent: Tuesday, January 17, 2012 11:46:50 AM
>> Subject: Re: [Novalug] question about variables ($...) (was: Re: question about ls)
>>
>>
>> Re: [Novalug] question about variables ($...) (was: Re: question about ls)
>> Monday, January 16, 2012 4:29 PM
>> From:----
>> "Jon LaBadie"<novalugml at jgcomp.com>
>> To:
>> novalug at calypso.tux.org
>>
>> Assuming you are using a Bourne style shell you can get a
>> list of all variables that are set with the "set" command.
>> Some of these have been "exported" and are considered to
>> be in your "environment" and will be passed on to other
>> commands you run. Your environment variables can be seen
>> with the "env" command.
>>
>> ----------
>>
>> In many years, I've not seen a good explanation of the
>> difference/relationship between variables as they exist
>> and the environment and/or shell.
>> I may not have read enough books. But I've seen a whole
>> boxcar of crap. Yours is the closest and
>> best to date in such a simple paragraph. (I've seen a
>> few meager attempts that almost always skirt the meat,
>> but never get to the T-bone. ).
>>
>> I was wondering if you would spend a little more of your
>> time and elaborate further on the existence of variables
>> when in or not in "the environment", or simply as "shell".
>>
>> I'll bet a LOT of people continue to use "set" to get a list,
>> or "env" to get a list and have no clue what or if there
>> is rhyme or reason why they use the different commands.
>>
>>
>> thanks!
>> Walt.........
>>
>>
>>
>> There's a shortage of technical workers for jobs that don't exist yet.
>> _______________________________________________
>> Novalug mailing list
>> Novalug at calypso.tux.org
>> http://calypso.tux.org/mailman/listinfo/novalug
>> _______________________________________________
>> Novalug mailing list
>> Novalug at calypso.tux.org
>> http://calypso.tux.org/mailman/listinfo/novalug
>>
>>>> End of included message<<<
More information about the Novalug
mailing list