how to capture and restore state of shell options

"Mark J. Reed" <[email protected]> Mon, 20 Oct 2025 16:19:50 -0400
Newsgroups gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user
Message-ID <CAA=-s3zGY6WY9PDiNLatZBTMr6fxywKUmJPdM_3HJ3n1J5m+tQ@mail.gmail.com>
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 <[email protected]>