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

Re: how to capture and restore state of shell options



Having now tried it, this doesn't quite work:

    saved_options=$(mktemp)
    set +o >$saved_options
    .... do stuff ...
    source $saved_options
    rm -f $saved_options

... because the output includes `set -o shinstdin`, but during `source` that option is unset and can't be turned on, so the file aborts at that point.  If I replace the `source` with `eval "$(<$saved_options)"`, that succeeds. 

On Mon, Oct 20, 2025 at 4:19 PM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
I thought this was meant to work:

    saved_options=$(set +o)
    .... do stuff ...
    eval "$saved_options"

But it doesn't. Inside command substitution, or even with output going to a pipe (though not with output going to a file), the `set +o` reports toggled options, including `set +o monitor` and `set +o zle`, which turn off job control and command-line editing after the "restoration".

The "not with output going to a file" part means this should work:

    saved_options=$(mktemp)
    set +o >$saved_options
    .... do stuff ...
    source $saved_options
    rm -f $saved_options

But is there a better way?

--
Mark J. Reed <markjreed@xxxxxxxxx>


--
Mark J. Reed <markjreed@xxxxxxxxx>


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