Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

RE: PATCH: 3.1.5: eval with exported vars

> I was going to say yes, but it's specifically mentioned in the source
> that it does this (exec.c, line 1793 of my current version):
> 	    if (cmd->vars) {
> 		/* Export this if the command is a shell function,
> 		 * but not if it's a builtin.
> 		 */
> 		addvars(cmd->vars, is_shfunc);

I just checked it with two versions of sh and ksh and all behave the same
way as zsh. I think, it is also correct.

Variable assignment does no _export_ them. It makes them available in
execution environment of command to be executed. It is just, that if command
executes in separate process, the only way to make them available is to
export them ...

In case of eval, FOO is set, but not exported, which is correct - and hence,
spawned process does not see it.

But zsh takes one thing wrong. Variable assignment made before special
builtins, remain in effect after builtin finishes (see POSIX and Single Unix
and Unix 98). eval is special builtin, but see

bor@itsrm2:/tools/var%> FOO=x eval 'echo $FOO'
bor@itsrm2:/tools/var%> echo $FOO

But as I understand, your patch blindly exports variable for every builtin?

> I think the idea must have been that `builtins don't need values
> exported' which, as you've seen, is not true in this case.  The patch
> is the simplest fix, and I think it's good enough.

Again, FOO=x cmd does not export it at all. something strange here.

> There's something that had me horribly confused (though it's actually
> working the way God intended):
> % FOO=x eval sh -c 'echo $FOO'
> <nothing printed>
> That's because eval sees the string "sh -c echo $FOO" and turns this
> into sh -c 'echo' 'x', so sh gets an 'echo' command with $0 set to x.
> To prove it:
> % FOO=x eval sh -c '"echo \$0" $FOO'
> x
> (why $0 not $1?) so you really need
> % FOO=x eval sh -c \''echo $FOO'\'
> x

Wow! May wonders never cease :)

bor@itsrm2:/tools/var%> FOO=x eval sh -c '"echo $FOO"'
bor@itsrm2:/tools/var%> echo $FOO


Still wrong - FOO disappears.

Messages sorted by: Reverse Date, Date, Thread, Author