Saturday, October 17, 2015

Re: system command takes a different environment variable from current shell?

On Fri, Oct 16, 2015 at 6:27 PM, Karl (Xiangrong) Cai <xcai@juniper.net> wrote:
> Hi,
> I have a login tcsh which originally has this environment variable c set as
> "/volume/current":
> <<<
> 601% env | grep "SHELL\|current"
> SHELL=tcsh
> c=/volume/current
> XTERM_SHELL=/bin/tcsh
>>>>
>
> And in vim, when I do 1) echo $c, and 2) let x = system("echo $c"); and then
> "echo x", both shows "/volume/current", which is expected;
>
> Then inside this shell I set this variable to another value:
> <<<
> 603% setenv c /volumeNEW/current
> 604% env | grep "SHELL\|current"
> SHELL=tcsh
> c=/volumeNEW/current
> XTERM_SHELL=/bin/tcsh
>>>>
>
> And now in in vim, when I do "echo $c" it shows the new value
> "/volumeNEW/current", but when I use the above mentioned 2nd way, it shows
> the old value "/volume/current". Is it supposed to show the new value?
>
> Am I wrong about how system() is supposed to pick up environment variable?
> Or there is a bug here?
>
> Thanks.
> --Karl

It isn't a bug, it's a feature.

When a new shell is started (e.g. by the :shell command, by system(),
etc.) it receives a brand-new environment which is initialized with a
copy of the environment of the mother program. Repeat: NOT the
original environment, but a COPY.

When that (sub)shell terminates, the memory used by it and by its
environment is freed (returned to the system) and the mother program
is resumed with its unmodified environment.

The same happens in the shell, when you start a new shell either
explicitly (by invoking bash, csh, ksh, zsh, etc., as a command with
or without parameters) or implicitly (e.g. in bash by enclosing one or
more successive commands in parentheses). When the subshell
terminates, its environment is returned to the system and the mother
program continues with its environment unmodified.


Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: