bash-preexec icon indicating copy to clipboard operation
bash-preexec copied to clipboard

no preexec call for subshells

Open dseomn opened this issue 10 months ago • 3 comments

Similar to #6, I'm not seeing preexec calls for subshells. That was supposed to be fixed in #22 though, right?

* dseomn@solaria:~$ preexec() { echo "command: $1"; }
* dseomn@solaria:~$ true
command: true
* dseomn@solaria:~$ (true)
* dseomn@solaria:~$

dseomn avatar Jan 09 '25 04:01 dseomn

That was supposed to be fixed in #22 though, right?

It caused the problem reported in #25, so it was turned off by default in commit 6db22a55d0fcefd3cd137ccbea6484702b351c06. Bash >= 5.0 are free from the reported problem, so if you want to enable it, you can set a non-empty value to a global shell variable __bp_enable_subshells.

However, __bp_enable_subshells=1 has another issue. See the following examples:

$ (echo 1; echo 2); echo 3; echo 4
1
2
preexec        <-- We wanted this before "1"
3
4
precmd
$ __bp_enable_subshells=1 bash
$ (echo 1; echo 2); echo 3; echo 4
preexec        <-- Expected. Now, we have preexec at the beginning
1
preexec        <-- We do not want this
2
preexec        <-- We do not want this
3
4
precmd
$

"PS0 & signal" or "PS0 & funsub" discussed in #28 should provide a better solution.

akinomyoga avatar Jan 09 '25 08:01 akinomyoga

Could the multiple preexec issue be solved in bash 5.0, 5.1, and 5.2 with a new internal variable to suppress it? Something like this before calling preexec:

if [[ "$__bp_done_preexec" = "1" ]]; then
  return
fi
__bp_done_preexec=1
...  # call preexec

And in precmd:

__bp_done_preexec=0

dseomn avatar Jan 09 '25 18:01 dseomn

Could the multiple preexec issue be solved in bash 5.0, 5.1, and 5.2 with a new internal variable to suppress it?

We are already doing this with the variable __bp_preexec_interactive_mode (__bp_preexec_interactive_mode=on corresponds to your __bp_done_preexec=0 and __bp_preexec_interactive_mode="" corresponds to your __bp_done_preexec=1). However, it is intentionally turned off inside subshells for some historical reasons:

https://github.com/rcaloras/bash-preexec/blob/8926de0a1f69d17f5b05099cfaaa52d833084c2f/bash-preexec.sh#L234-L242

The second preexec (before echo 2) may be suppressed by removing the requirement [[ 0 -eq "${BASH_SUBSHELL:-}" ]]. I thought there was a discussion about this, but I do not find it now. Maybe my memory is incorrect.

The third preexec (before echo 3) cannot be fixed by the internal-variable approach because the changes to the variable in subshell is invisible from the parent shell.

akinomyoga avatar Jan 09 '25 22:01 akinomyoga