Sunday, October 18, 2015

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

2015-10-18 5:20 GMT+03:00 Tony Mechelynck <antoine.mechelynck@gmail.com>:
> 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.

No. OP said that `echo $c` in Vim echoes new value and `let
x=system('echo $c')|echo x` shows different value *in the same Vim
instance*. This *cannot* happen due to subshell that freed memory: if
it was the case `echo $c` would use the old value as well, and
system() inherits environment from Vim.

>
> 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.

--
--
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: