printf %s\\n "$-"
Will list the single letter options in a single string.
That parameter can also be used like:
set -f -- ${-:+"-$-"}
echo *don\'t* *glob* *this*
set +f "$@"
To first disable shell -f
ilename expansion while simultaneously saving a value for $-
- if any - in $1
. Next, no globs occur, and last +f
ilename expansion is once again enabled, and possibly also disabled.
For example, if -f
ilename expansion was already disabled when the value for $-
was first saved, then its saved value would be (at least):
f
And so when set
is run again, it works out to:
set +f -f
Which just puts you right back where you started.
set +o
Will list all set
table shell options (see Jason's answer for the shopt
able - is that a word? - options) in a form that is safe for shell reentry. In that way, you can also do:
state=$(set +o)
set -some -crazy -options
eval "$state"
To save, change, and restore the shell options' state respectively.
To handle shopt
ions and set
table options in one go:
state=$(set +o; shopt -p)
#do what you want with options here
eval "$state"
You can also call set
without any arguments to add a list of all of the shell's currently set variables - also quoted for reentry to the shell. And you can - in bash - additionally add the command typeset -fp
to also include all currently declared shell functions. You can lump it all together and eval
when ready. You can even call alias
without arguments for more of the same. That... might cover it, though. I guess there is "$@"
- which you'd have to put in a bash
array first, I suppose, before doing set
.
Nope, there's also trap
. This one's a little funny. Usually:
trap 'echo this is my trap' 0
(echo this is my subshell; trap)
...will just print this is my subshell because the subshell is a new process and gets its own set of trap
s - and so doesn't inherit any trap
s but those which its parent has explicitly ignored - (like trap '' INT
).
However:
trap 'echo this is my trap' 0
save_traps=$(trap)
trap
behaves specially when it is the first and only command run in a command substitution subshell in that it will reproduce a list of the parent shell's currently set traps
in a format which is quoted for safe reentry to the shell. And so you can do the save_traps
, then set
without arguments - and all of the rest already mentioned - to pretty much get a lock on all shell state. You might want to explicitly add export -p
and readonly -p
to restore original shell var attributes, though.
Anyway, that's enough.