4
$ echo $-
himBHs
$ set -h
$ set -i
bash: set: -i: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
$ set -m
$ set -B
$ set -H
$ set -s
bash: set: -s: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

As a result I can't use this method to restore bash state using

oldstate="$(set +o); set -$-"                # POSIXly store all set options.
.
.
set -vx; eval "$oldstate"         # restore all options stored.

as described in this Unix & Linux answer

$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic

EDIT/CONCLUSION: As the answer below informs, the content of $- includes settings having nothing to do with set, despite the set man entry stating

The current set of options may be found in $-.

which is true, but confusing by omission.

When recording state, use of $- is not required because all the options are already able to recorded for resetting with

% restoreState=$(shopt  -po ; shopt -p)

or more selectively for a subset of options, e.g.,

shopt  -po | grep -E 'brace|glob' ; shopt -p | grep glob

Craig Hicks
  • 809
  • 7
  • 18
  • 1
    Strangely, my Bash version (4.3.48, on 16.04) accepts `set -i` without complaining, even though it is not documented in `help set`, but `set -s` fails just like in your case. – Byte Commander Jun 28 '20 at 18:00
  • @ByteCommander - Good idea to mention version info. – Craig Hicks Jun 28 '20 at 18:08
  • There's [a comment about this](https://unix.stackexchange.com/questions/310957/how-to-restore-the-value-of-shell-options-like-set-x/310963#comment1056405_310963) on the linked post - my guess is it simply doesn't make sense to try to set an already running shell to be interactive (or to read commands from stdin?) from within – steeldriver Jun 28 '20 at 18:16
  • @steeldriver - Missed that one - thank! That leaves 's'. – Craig Hicks Jun 28 '20 at 18:23
  • 2
    What is your end goal? I guess you could replace `set -$-` with `set -${-//[is]}` for use interactively – steeldriver Jun 28 '20 at 18:27
  • @steeldriver - Very helpful. In the end, I'm not sure whether the settings in `$-` aren't already covered by `shopt -po` and `shopt -p`, in which case `$-` isn't really necessary. – Craig Hicks Jun 28 '20 at 18:42

1 Answers1

4

Note that the bash man page, right near the top, says

OPTIONS

All of the single-character shell options documented in the description of the set builtin command, including -o, can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked:

And the goes on to describe -i and -s (in addition to the perhaps more well-known -c and -l)

"In addtion" being the key bit there.

Relevant chapters from the Bash Reference Manual:

  • Invoking Bash: discusses the bash -i option (force the shell to run interactively) and the bash -s option.

  • Interactive Shell

  • Special Parameters: $-: explains the meaning of $-:

    Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

  • The Set Builtin: lists further options which may appear in $-, such as -h, -m, -B, -H (compare to your example himBHs).

Abdull
  • 362
  • 2
  • 6
  • 13
glenn jackman
  • 17,625
  • 2
  • 37
  • 60