9

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?

1

2 Answers 2

10

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
2
  • Thanks. What are the differences between "the current option flags as specified upon invocation" and "those set by the shell itself (such as the -i option)"?
    – Tim
    Commented Dec 6, 2015 at 4:41
  • The latter are inherent properties of the shell that it exposes to you. You don't have to ask for -i to be in an interactive shell, or to be disabled in a non-interactive shell. Some of the others, like c, are more interesting - you can't set -c at run time at all, but bash -c 'echo $-' will include the flag. Commented Dec 6, 2015 at 4:48
4

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

2
  • And in programs that take a list of filenames to process, a single - as an argument often means "process what's on stdin at this point in the list".
    – zwol
    Commented Dec 6, 2015 at 16:37
  • @zwol - maybe...
    – mikeserv
    Commented Dec 6, 2015 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .