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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x