Open HaleTom opened 8 years ago
The before-command hook in bash trap ... DEBUG
causes many state changes.
zsh seems to work okay, but not always an option.
Perhaps bash needs shell commands for pushing/popping state (somewhat fine-grained) so that only specified target variables are mutated?
$?
is fine, It's only $_
.
I raised this as a question on SO.
It's possible that this is a bash
bug, see the zsh
example working at: bash: preserve $_ in a DEBUG trap
Solution given on SO, I'll make a PR.
The SO answer seems to be incomplete as only the last argument is captured in old_
.
bash-4.4$ f() { local old_="$1"; echo "=== $old_ ==="; : "$old_"; }; trap 'f "$_"' DEBUG
bash-4.4$ ls -lart >/dev/null
=== DEBUG ===
bash-4.4$ echo "$_"
=== -lart ===
-lart
bash-4.4$
Although s/1/@/
doesn't help, there might be a solution or there's a bash bug/unhandled edge-case "feature."
@steakknife, the bash
manual says:
expands to the last argument to the previous command, after expansion
So I assert that the SO answer is correct.
In
bash
,$_
should be “the last argument of the last command executed”Instead, this is overwritten: