From: Name withheld by request on
In article <735501335.526405(a)irys.nyx.net>,
Name withheld by request <anonb6e9(a)nyx3.nyx.net> wrote:
>Bug or feature? Pls explain:
> /tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"'
> x: /4/

Is this right?:

The 'env' process passes x to the start up
environment of bash. All variables in the start up
environment of any UNIX shell are exported to processes
started by that shell.

I think my confusion comes from being a shell
user, not a developer. 'man execle' suggests to
me, a process can be started w/or w/o an initial
set of env vars.

--
'man environ' :

A C program can manipulate its environment using the functions
getenv(3), putenv(3), setenv(3), and unsetenv(3).

Of course a shell can change it's own env vars. The
"exported variable" feature is specific to shells only, right?

If a C program wants to pass an env var to a process
it can not just "mark env vars" as exported, it has to
pass them through for example 'execle', right?

--
thx
From: Alan Curry on
In article <735505438.900277(a)irys.nyx.net>,
Name withheld by request <anonb6e9(a)nyx.net> wrote:
>
> I think my confusion comes from being a shell
> user, not a developer. 'man execle' suggests to
> me, a process can be started w/or w/o an initial
> set of env vars.

execve or execle can be called with an envp that is unrelated to the current
environment of the calling program. In that case, the new program will get
the new set of environment variables instead of inheriting the previous
program's environment. One possible use of this ability would be to specify
an empty envp, which would start the new program with an empty environment.

> Of course a shell can change it's own env vars. The
> "exported variable" feature is specific to shells only, right?

The export concept is limited to shells because the shells are the only
commonly used languages in which the "regular" variables are similar enough
to environment variables that it makes sense to move a variable from one list
to the other.

Also the shell parameter expansion syntax "$x" doesn't distinguish between
the two types, so shell programming gives you a way to request substitution
of the-value-of-shell-variable-x-or-environment-variable-x-whichever-exists
but doesn't provide any easy way to request one of them tell which one you got.

And most confusing, as you have found, are assignments in the shell. If and
only if the variable you're assigning to already exists in the environment
(in other words, if it's exported), the assignment modifies the environment.

>
> If a C program wants to pass an env var to a process
> it can not just "mark env vars" as exported, it has to
> pass them through for example 'execle', right?

In C, since there's no "$x" variable expansion, there's no confusion between
normal variables and environment variables. If you access an environment
variable, you'll know it because you'll be using a function with "env" in the
name, like getenv(), or the special variable "environ".

As for passing environment variables through exec, the normal thing to do in
C is to pass the current environment along, either by using the "environ"
variable explicitly in the call to execve or execle, or by calling execvp,
execlp, execv, or execl instead, all of which do it implicitly.

--
Alan Curry
From: Bill Marcum on
On 2010-05-16, Name withheld by request <anonb6e9(a)nyx.net> wrote:
> In article <735501335.526405(a)irys.nyx.net>,
> Name withheld by request <anonb6e9(a)nyx3.nyx.net> wrote:
>>Bug or feature? Pls explain:
>> /tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"'
>> x: /4/
>
> Is this right?:
>
> The 'env' process passes x to the start up
> environment of bash. All variables in the start up
> environment of any UNIX shell are exported to processes
> started by that shell.
>
The second bash command is executed as a child of the first,
after it has set x=4.

From: Name withheld by request on
Thanks Alan for the help - I will be saving your response
for reference.

In article <hso0eo$ck$1(a)speranza.aioe.org>,
Alan Curry <pacman(a)kosh.dhis.org> wrote:
>In article <735505438.900277(a)irys.nyx.net>,
>Name withheld by request <anonb6e9(a)nyx.net> wrote:
>>
>> I think my confusion comes from being a shell
>> user, not a developer. 'man execle' suggests to
>> me, a process can be started w/or w/o an initial
>> set of env vars.
>
>execve or execle can be called with an envp that is unrelated to the current
>environment of the calling program. In that case, the new program will get
>the new set of environment variables instead of inheriting the previous
>program's environment. One possible use of this ability would be to specify
>an empty envp, which would start the new program with an empty environment.
>
>> Of course a shell can change it's own env vars. The
>> "exported variable" feature is specific to shells only, right?
>
>The export concept is limited to shells because the shells are the only
>commonly used languages in which the "regular" variables are similar enough
>to environment variables that it makes sense to move a variable from one list
>to the other.
>
>Also the shell parameter expansion syntax "$x" doesn't distinguish between
>the two types, so shell programming gives you a way to request substitution
>of the-value-of-shell-variable-x-or-environment-variable-x-whichever-exists

x with the value 9 remained in the process environment of the 1st
bash shell, while the "shell variable" got the value 4; when the
2nd shell with the echo was created the first shell passed the
value 4 for x ( it was exported since it started in the process
env of the 1st shell ).

>but doesn't provide any easy way to request one of them tell which one you got.
>
>And most confusing, as you have found, are assignments in the shell. If and
>only if the variable you're assigning to already exists in the environment
>(in other words, if it's exported), the assignment modifies the environment.

Nicely expressed, thank you.

>>
>> If a C program wants to pass an env var to a process
>> it can not just "mark env vars" as exported, it has to
>> pass them through for example 'execle', right?
>
>In C, since there's no "$x" variable expansion, there's no confusion between
>normal variables and environment variables. If you access an environment
>variable, you'll know it because you'll be using a function with "env" in the
>name, like getenv(), or the special variable "environ".
>
>As for passing environment variables through exec, the normal thing to do in
>C is to pass the current environment along, either by using the "environ"
>variable explicitly in the call to execve or execle, or by calling execvp,
>execlp, execv, or execl instead, all of which do it implicitly.
>
>--
>Alan Curry

thanks again,
Tom