zsh-workers
 help / color / mirror / code / Atom feed
* Issue trying to save and restore options
@ 2018-05-23 15:54 Alexandre Rames
  2018-05-23 18:31 ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandre Rames @ 2018-05-23 15:54 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 959 bytes --]

Hello,

As part of a script, I was trying to save and restore options in a
shell-agnostic way. The goal is to write

```
# Save options.
set -x
# Modify other options.
# Do stuff.
# Restore options
```

Googling, I came across this stack overflow answer
<https://unix.stackexchange.com/questions/310957/how-to-undo-a-set-x/310963?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa>,
suggesting the use of `set +o`.
However this gives me unexpected results

% zsh --no-globalrcs --no-rcs
username% zsh --version
zsh 5.5.1 (x86_64-apple-darwin17.5.0)
username% set +o
set +o noaliases
# More option lines. See full log attached.
set -o zle
username% OPTS=$(set +o)
# Hitting Ctrl-C works as expected
username%
username%
username% eval "$OPTS"
# Hitting Ctrl-C now does not behave as expected
username% ^C^C^C

The same thing works in `bash` though.
Does someone know if that is expected to work? Is that a known issue with
zsh?

Alexandre

[-- Attachment #1.2: Type: text/html, Size: 1835 bytes --]

[-- Attachment #2: log --]
[-- Type: application/octet-stream, Size: 3684 bytes --]

% zsh --no-globalrcs --no-rcs
username% zsh --version
zsh 5.5.1 (x86_64-apple-darwin17.5.0)
username% set +o
set +o noaliases
set +o aliasfuncdef
set +o allexport
set +o noalwayslastprompt
set +o alwaystoend
set +o appendcreate
set +o noappendhistory
set +o autocd
set +o autocontinue
set +o noautolist
set +o noautomenu
set +o autonamedirs
set +o noautoparamkeys
set +o noautoparamslash
set +o autopushd
set +o noautoremoveslash
set +o autoresume
set +o nobadpattern
set +o nobanghist
set +o nobareglobqual
set +o bashautolist
set +o bashrematch
set +o nobeep
set +o nobgnice
set +o braceccl
set +o bsdecho
set +o nocaseglob
set +o nocasematch
set +o cbases
set +o cdablevars
set +o chasedots
set +o chaselinks
set +o nocheckjobs
set +o nocheckrunningjobs
set +o noclobber
set +o combiningchars
set +o completealiases
set +o completeinword
set +o continueonerror
set +o correct
set +o correctall
set +o cprecedences
set +o cshjunkiehistory
set +o cshjunkieloops
set +o cshjunkiequotes
set +o cshnullcmd
set +o cshnullglob
set +o nodebugbeforecmd
set +o dvorak
set +o emacs
set +o noequals
set +o errexit
set +o errreturn
set +o noevallineno
set +o noexec
set +o extendedglob
set +o extendedhistory
set +o noflowcontrol
set +o forcefloat
set +o nofunctionargzero
set +o noglob
set +o noglobalexport
set -o noglobalrcs
set +o globassign
set +o globcomplete
set +o globdots
set +o globstarshort
set +o globsubst
set +o nohashcmds
set +o nohashdirs
set +o hashexecutablesonly
set +o nohashlistall
set +o histallowclobber
set +o nohistbeep
set +o histexpiredupsfirst
set +o histfcntllock
set +o histfindnodups
set +o histignorealldups
set +o histignoredups
set +o histignorespace
set +o histlexwords
set +o histnofunctions
set +o histnostore
set +o histreduceblanks
set +o nohistsavebycopy
set +o histsavenodups
set +o histsubstpattern
set +o histverify
set +o nohup
set +o ignorebraces
set +o ignoreclosebraces
set +o ignoreeof
set +o incappendhistory
set +o incappendhistorytime
set -o interactive
set +o interactivecomments
set +o ksharrays
set +o kshautoload
set +o kshglob
set +o kshoptionprint
set +o kshtypeset
set +o kshzerosubscript
set +o nolistambiguous
set +o nolistbeep
set +o listpacked
set +o listrowsfirst
set +o nolisttypes
set +o localloops
set +o localoptions
set +o localpatterns
set +o localtraps
set +o login
set +o longlistjobs
set +o magicequalsubst
set +o mailwarning
set +o markdirs
set +o menucomplete
set -o monitor
set +o nomultibyte
set +o nomultifuncdef
set +o nomultios
set +o nonomatch
set +o nonotify
set +o nullglob
set +o numericglobsort
set +o octalzeroes
set +o overstrike
set +o pathdirs
set +o pathscript
set +o pipefail
set +o posixaliases
set +o posixargzero
set +o posixbuiltins
set +o posixcd
set +o posixidentifiers
set +o posixjobs
set +o posixstrings
set +o posixtraps
set +o printeightbit
set +o printexitvalue
set +o privileged
set +o promptbang
set +o nopromptcr
set +o nopromptpercent
set +o nopromptsp
set +o promptsubst
set +o pushdignoredups
set +o pushdminus
set +o pushdsilent
set +o pushdtohome
set +o rcexpandparam
set +o rcquotes
set -o norcs
set +o recexact
set +o rematchpcre
set +o restricted
set +o rmstarsilent
set +o rmstarwait
set +o sharehistory
set +o shfileexpansion
set +o shglob
set -o shinstdin
set +o shnullcmd
set +o shoptionletters
set +o noshortloops
set +o shwordsplit
set +o singlecommand
set +o singlelinezle
set +o sourcetrace
set +o sunkeyboardhack
set +o transientrprompt
set +o trapsasync
set +o typesetsilent
set +o nounset
set +o verbose
set +o vi
set +o warncreateglobal
set +o warnnestedvar
set +o xtrace
set -o zle
username% OPTS=$(set +o)
username%
username%
username% eval "$OPTS"
username% ^C^C^C

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Issue trying to save and restore options
  2018-05-23 15:54 Issue trying to save and restore options Alexandre Rames
@ 2018-05-23 18:31 ` Daniel Shahaf
  2018-06-06 18:27   ` Alexandre Rames
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Shahaf @ 2018-05-23 18:31 UTC (permalink / raw)
  To: Alexandre Rames, zsh-workers

Alexandre Rames wrote on Wed, 23 May 2018 08:54 -0700:
> username% OPTS=$(set +o)

The 'set' runs inside a subshell so it sees different options:

[[[
% set +o >1
% printf '%s' "$(set +o)" >2
% diff 1 2 
116c116
< set -o monitor
---
> set +o monitor
177c177
< set -o zle
---
> set +o zle
]]]

(The 'zle' option controls whether the line editor is enabled.)

So, you could try capturing the output of 'set +o' using redirections
rather than command substitutions; that should work and remain shell-
agnostic.  (If you want a shell-specific solutions there are other ways,
e.g., check $ZSH_VERSION and then use ${(kv)options} to save the
values.)

As to whether the behaviour of +o in a subshell should be changed, I'll
leave that for others.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Issue trying to save and restore options
  2018-05-23 18:31 ` Daniel Shahaf
@ 2018-06-06 18:27   ` Alexandre Rames
  0 siblings, 0 replies; 3+ messages in thread
From: Alexandre Rames @ 2018-06-06 18:27 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1152 bytes --]

Sorry for the late reply.

Thanks for the help Daniel. Using redirection indeed works.

Here is what I used, in case it is useful to others:

OPTS_BACKUP=$(mktemp)
set +o > "$OPTS_BACKUP"
set -x

# More stuff

set +vx
RESTORE_STATE=$(cat "$OPTS_BACKUP")
eval "$RESTORE_STATE"

On Wed, May 23, 2018 at 11:31 AM Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:

> Alexandre Rames wrote on Wed, 23 May 2018 08:54 -0700:
> > username% OPTS=$(set +o)
>
> The 'set' runs inside a subshell so it sees different options:
>
> [[[
> % set +o >1
> % printf '%s' "$(set +o)" >2
> % diff 1 2
> 116c116
> < set -o monitor
> ---
> > set +o monitor
> 177c177
> < set -o zle
> ---
> > set +o zle
> ]]]
>
> (The 'zle' option controls whether the line editor is enabled.)
>
> So, you could try capturing the output of 'set +o' using redirections
> rather than command substitutions; that should work and remain shell-
> agnostic.  (If you want a shell-specific solutions there are other ways,
> e.g., check $ZSH_VERSION and then use ${(kv)options} to save the
> values.)
>
> As to whether the behaviour of +o in a subshell should be changed, I'll
> leave that for others.
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-06-06 20:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-23 15:54 Issue trying to save and restore options Alexandre Rames
2018-05-23 18:31 ` Daniel Shahaf
2018-06-06 18:27   ` Alexandre Rames

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).