Meaning of a hyphen as a special parameter?

From https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html,

- (A hyphen.) 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).

I am not sure what it says. Can you give some examples for the meaning of a hyphen parameter?

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

You can echo $- to see the currently-enabled shell options:

$ echo $-
himBH

Those are options you can provide with a - on shell invocation — bash -h -B — or later on using set.

The flags are defined in the documentation for set. My options above are:

  • -h
    Locate and remember (hash) commands as they are looked up
    for execution. This option is enabled by default.
  • -m
    Job control is enabled (see Job Control). All processes
    run in a separate process group. When a background job
    completes, the shell prints a line containing its exit status.
  • -B
    The shell will perform brace expansion (see Brace Expansion).
    This option is on by default.
  • -H
    Enable ‘!’ style history substitution (see History
    Interaction
    ). This option is on by default for interactive shells.

and i for an interactive shell.

If I change the active options, say with set -x, I’ll get a new value of $-:

$ set -x +h
$ echo $-
imxBH

Method 2

It depends – as a dollar-sign expansion, $- expands to a list of the current shell’s settable single-letter options – such as -x and -f and -C. For an example, an interactive shell will expand it at least like:

echo "$-"

i

The longer, set -o option versions can be had with set +o.

But there is another kind of hyphen-special-parameter which is a sort of analog to this. You can use two consecutive hyphens to signal the end of options in a typical command’s argument list, but you can also use a single-hypen to do the same for a POSIX-shell. Historically, shells accepted a single hyphen to mean much the same.

A bash shell interprets a single-hyphen specially in argument list-contexts. With set, for example it marks the end of options and disables -verbose and -xtrace. Additionally, set - does not clear a parameter list if it is the first and only argument to set as set -- would.

A login shell will often append a - to its argv[0].


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