On 2024-07-20 05:56, Greg Wooledge wrote:
On Sat, Jul 20, 2024 at 05:46:23 +0800, p...@gmx.it wrote:
$ VAR1=foo && ./a.sh
$ export VAR2=foo; ./a.sh
$ ./b.sh


$VAR1 will be seen by a.sh only, but $VAR2 can be seen my current
login
session (such as b.sh). Am I right? I am a bit confused about env
scope.

If we assume NO other commands have been executed in this shell so far,
then:

VAR1 is not marked for export.  It's just a regular shell variable.
It won't be seen by either call to ./a.sh which is a (non-subshell)
child process, not will it be seen by ./b.sh which is also a
non-subshell
child.

VAR2 is marked for export by the interactive shell.  It's a permanent
part of the shell's environment and will be seen by all child processes
from that point forward.

VAR2 will therefore be seen by the second call to ./a.sh and the call
to ./b.sh.

I verified that as follows.

$ VAR=foo ./a.sh
i can see VAR=foo

$ ./a.sh
i can see VAR=

$ VAR=foo; ./a.sh
i can see VAR=

$ export VAR=foo; ./a.sh
i can see VAR=foo

Thanks Greg.


Now, what you didn't ask, but what I *expected* you to ask, is:

    What's the difference between these two commands?
        VAR3=foo ./a.sh
        VAR3=bar; ./a.sh

In the first command, VAR3 is placed in the environment of the command
being executed.  ./a.sh will see it.  VAR3 will not survive beyond
this command.  It will be discarded, and future commands will not be
aware it ever existed.

In the second command, VAR3 is created as a regular variable in the
current shell, but not exported to the environment.  It will NOT be
seen by ./a.sh, but it WILL be seen by future shell commands within
this session.

I can not clearly understand for this statement. what's "future shell
commands"? can you show an example?

regards,
timothy

Reply via email to