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:
Example output:
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:
Make it executable and run it:
Typical output:
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:
Run the updated script:
Now you should see:
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


Further Reading


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

Watch Video