Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • What happens, if you do a zsh -c 'echo $FOO' (use single quotes!) instead? Can you see it then? Commented Jul 11, 2017 at 4:45
  • The correct value is printed from a new sub shell (tested for bash child also). Clearly the environment is persistent somehow as the child can inherit it but why doesn't the parent honour it? Commented Jul 11, 2017 at 14:07
  • 3
    That's what I thought. I guess the shell has somewhere a symbol table of variables, some of them are marked as "exported", which means that upon opening a subshell, they are placed into the environment of the child process. Initially (when the shell starts), the variables from the environment at that time are copied into the symbol table (of course also as "exported" variables). When you change the environment, the shell doesn't get noticed to update their symbol table - but child processes (like env) see the modified environment. Commented Jul 12, 2017 at 6:09
  • 2
    I tested on Ubuntu 16.04 with zsh 5.1.1 and bash 4.3.48(1) and it seems setting an environment variable for zsh in GDB does not make it visible as a shell variable but does cause it to be passed on to child processes (as you have observed), while setting one for bash does make it visible as a shell variable but does not cause it to be passed on to child processes! It looks like zsh and bash use different strategies for managing variables, with zsh tracking non-environment variables and bash storing everything in its environment which it sanitizes when launching a (non-subshell) child. Commented Jul 19, 2017 at 16:48
  • @EliahKagan, interesting; you should post that as an answer. I also wonder if it makes a difference if you run export FOO in bash? Commented Jul 27, 2017 at 23:48