how to understand the output of "echo $-"?

They represent the values of the shell's flags; this is defined by POSIX:

-

(Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invocation, by the set special built-in command, or implicitly by the shell.

The Zsh manual mentions it briefly:

- <S> Flags supplied to the shell on invocation or by the set or setopt commands.

as does the Bash manual in the description of set:

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

To understand the output of echo $- you need to look up the options in your shell's manual. For example, in Bash, echo $- outputs himBHs for me, which means that the -h, -m, -B and -H options are enabled (see help set for details), that the shell is interactive (-i) and reading from standard input (-s).


From the Bash Reference Manual:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]

Without options, the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the currently-set variables.

...

The options are off by default unless otherwise noted. Using + rather than - causes these options to be turned off. The options can also be specified as arguments to an invocation of the shell. The current set of options may be found in $-. The return status is always true unless an invalid option is encountered.

Tags:

Bash

Variable