5
$ 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

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
  • 734
glenn jackman
  • 18,218