I am trying to build a pre-exec checker in bash (using bash-preexec.sh
)
In order to fail the command when the pre-exec hook fails, I need to run shopt -s extdebug
(which fails the command when trap DEBUG command returns non-zero RC).
If I run shopt -s extdebug
in my shell as a command, this works perfectly fine (the preexec checker just grepped for "OK" in a file containing word "FAIL", so command shouldn't execute):
> grep shopt .bashrc
> bash
bash-4.1$ echo TEST2
TEST2 <=== Expected, since I didn't turn on extdebug yet
bash-4.1$ shopt -s extdebug > /dev/null 2>&1
bash-4.1$ echo TEST2
bash-4.1$ # WHAT WAS EXPECTED. Command failed to execute.
However, when I add the command to my .bashrc, it has no effect:
> grep shopt .bashrc
shopt -s extdebug > /dev/null 2>&1
> bash
bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
bash: warning: cannot start debugger; debugging mode disabled
bash-4.1$ echo TEST2
TEST2
OK, figured out the answer.
is required (in addition to
shopt -s extdebug
) to have the functionality work in sub-shells. And somehow,.bashrc
is treated as supershell of actual interactive shell, in a sense, if you don't add this, it won't take effect on interactive command prompt.So, you need to add both command to
.bashrc