Bash – Meaning of a Hyphen as a Special Parameter

bash

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?

Best Answer

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