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

Re: 'emulate sh -c' and $0

On May 29,  7:04pm, Richard Hansen wrote:
} I just encountered what I think is a bug in Zsh 5.0.5.

To the extent that it's working exactly as documented, it's not a bug ...

}     zsh -c '
}         emulate sh -c "echo \"\$0\""
}         bar() { emulate sh -c "echo \"\$0\""; }
}         bar
}     ' foo arg1
} I expected it to produce:
}     foo
}     foo

If you throw in "unsetopt functionargzero" any time before calling "bar" 
then it does produce that output.

If you rewrite your example as

    zsh -c '
        emulate sh -c "echo \"\$0\""
        emulate sh -c "bar() { echo \"\$0\"; }"
    ' foo arg1

then it also produces your expected output; you just need to define the
function in the right scope.

} This is relevant when sourcing a file containing (POSIX) sh code that
} might examine $0 (e.g., for logging or to 'exec "$0" "$@"' after
} exporting/unsetting environment variables).
} Perhaps Zsh should save the original value of $0 somewhere and restore
} it when entering sh emulation mode.

I don't find those examples particularly compelling, but the original
value of $0 is already stashed; what would need to change is that the
*local* value of $0 gets temporarily replaced by the global one.  (The
[un]setting of functionargzero controls whether a local value is ever
created in the first place.)

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