4

My question is best explained with an example.

set -x or set -v "turn on" features

set +x or set +v "turn off" features

But if my script calls another script or shell function, in that other script/function, how do I get the status of these settings. e.g.,

    # In called script/function
    # <save status of "set">

    set -abcdef 

    # do stuff

    # <restore status of "set">

Thanks!

1
  • 1
    The $- parameter contains a set of single-letter option flags (try echo $-; it produced himBH for me). Commented Aug 26, 2014 at 3:43

1 Answer 1

1

Here's the simple solution:

# In called script/function
(
  set -abcdef 
  # do stuff
)

The (...) is a subshell; when that subshell finishes, its environment dies with it, and you're back to the environment at entry.

If that's too drastic, you can make a copy of $- or $SHELLOPTS to keep the current options (SHELLOPTS includes some options which don't have single character abbreviations, so it is more general), and then laboriously restore from that.

Sign up to request clarification or add additional context in comments.

2 Comments

Might make sense to mention set +o and shopt -p to make the restore case slightly less laborios... unless you know of reasons those don't work.
Unfortunately, all variables set in ( subshell ) will also be lost. It does not fit source-able scripts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.