Re: how to capture and restore state of shell options

"Mark J. Reed" <[email protected]> Mon, 20 Oct 2025 16:27:28 -0400
Newsgroups gmane.comp.shells.zsh.user
Message-ID <CAA=-s3xOAPENRGLVUWmU1MEwGYXrao-Xi=COmBgCgWySJU2G+g@mail.gmail.com>
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 <[email protected]> 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 <[email protected]>
>


-- 
Mark J. Reed <[email protected]>