Is there a Bash command to figure out if `set -e` was executed?
Jul 20, 7:50pm
Yes, in Bash, you can check whether the -e option (which causes the script to exit immediately if any command exits with a non-zero status) is set by examining the current shell options. Here are a few ways to do it:
1. Using set +o or set -o
You can check the current state of the -e option (also known as errexit) using the following command:
bash
set +o | grep errexit
If errexit is off, it will appear as errexit off.
If errexit is on, it will appear as errexit on.
Alternatively, you can use:
bash
set -o | grep errexit
2. Using $-
The special variable $- contains the current set of options for the shell. You can check if e is present in this string:
bash
echo $- | grep -q 'e' && echo "set -e is ON" || echo "set -e is OFF"
If set -e is enabled, the output will include e in the string (e.g., himBHe).
If set -e is disabled, the output will not include e.
3. Using shopt (for Bash-specific options)
While shopt is typically used for Bash-specific options, set -e is a standard shell option, so the above methods are more direct.
Jul 20, 7:50pm
This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.