Skip to main content
The $- variable in Bash shows the current shell’s option flags. These flags differ when you run commands interactively versus inside a script. In this guide, we’ll:
  • Inspect $- interactively
  • Compare interactive vs. non-interactive outputs
  • Demonstrate how set -e affects $-
  • Review common $- flags in a summary table

Checking $- Interactively

Run the following command in your terminal to see which flags are active in your interactive shell:
echo $-
Example output:
himBHs
Each letter (such as h, i, m, B, H, s) represents a specific shell option. These may vary between systems and user configurations.
The exact set of flags depends on your Bash version and how you’ve configured your environment (e.g., via .bashrc or .bash_profile).

Comparing with a Non‐Interactive Script

Create a file named special-dash.sh:
#!/usr/bin/env bash
echo $-
Make it executable and run it:
chmod +x special-dash.sh
./special-dash.sh
Typical output:
hB
You’ll notice the i flag (interactive mode) is missing because scripts run non-interactively by default.

Enabling “Exit on Error” with set -e

The set -e option causes Bash to exit immediately if any command returns a non-zero status. Let’s enable it in our script:
#!/usr/bin/env bash
set -e
echo $-
Run the updated script:
./special-dash.sh
Now you should see:
ehB
The leading e indicates that “exit on error” is active.
Using set -e can make debugging harder if you’re not careful. Always test scripts thoroughly or combine with set -u and set -o pipefail for stricter error checking.

Common $- Flags

FlagDescription
iShell is running in interactive mode
eExit immediately if a command exits with failure
uTreat unset variables as an error
xPrint commands and their arguments as they execute

Further Reading


By understanding and inspecting the $- variable, you can better control your shell’s behavior in both interactive sessions and automated scripts.