bash – How to Understand the Output of `echo $-`

bashvariable

How to understand the output of echo $-? It looks like some kind of flag characters. I can't get a clue by googling.

Best Answer

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).

Related Question