Comments (9)
There's a workaround though (https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap):
trapfunc() { local old_=$1; date; : "$old_"; }
trap 'trapfunc "$_"' DEBUG
from pure-bash-bible.
from pure-bash-bible.
Behavior without trap:
$ echo hello
hello
$ echo $_
hello
Behavior with a trap:
$ trap date debug
$ echo hello
Sun Apr 14 22:03:57 CEST 2019
hello
$ echo $_
Sun Apr 14 22:04:01 CEST 2019
date
In the last line, the user expected to get "hello", not "date".
Behavior with a trap, using the workaround:
$ trapfunc() { local old_=$1; date; : "$old_"; }
$ trap 'trapfunc "$_"' DEBUG
$ echo hello
Sun Apr 14 22:06:00 CEST 2019
hello
$ echo $_
Sun Apr 14 22:06:02 CEST 2019
hello
from pure-bash-bible.
from pure-bash-bible.
What I recommend is simply a note in the README that explain this limitation / behavior.
And I think using "$*"
instead of $1
is wrong, because you now return all the previous arguments as one string, instead of only the last. "$@"
would be better, but it's not really useful either. It also depends on how you set your trap.
from pure-bash-bible.
Are you still interested in an additional note in the TRAPS section of the README? I can send a PR. Otherwise I'll close this issue 🙂
from pure-bash-bible.
When writing doc, it's important to understand the reader may have a different objective and they definitely have a different perspective. So there is a good chance the perceived "solution" is probably different too. I'm interested in your function, but your comments assume my objective and perspective is the same as yours. The following goes a long way to transparency and facilitates collaboration. Communicate:
- desired behavior (input and utility of output)
- expected output
- actual output
- the solution
Application of
If you assume the actual behavior is expected, and explain why your patch is an improvement, it helps the reader understand you; and, a great side effect is, meaningful feedback such as "why the actual behavior is desirable," becomes possible.
from pure-bash-bible.
Well, the discussion
label was removed to leave only the bug
one, so I thought this was clear, but I'll try again to explain.
- The
$_
variable is populated with the last argument of last command:
$ echo hello
hello
$ echo $_
hello
- But when using a trap on DEBUG, the "last command" becomes the last command in the trap. Therefore it can be surprising. It is expected (i.e. it is not a Bash bug), but surprising, and in my opinion this behavior deserves a note.
$ echo hello
hello
$ echo $_
hello # OK
$ trap date debug
$ echo hello
Sun Apr 14 22:03:57 CEST 2019
hello
$ echo $_
Sun Apr 14 22:04:01 CEST 2019
date # user was probably expecting "hello", not "date"
Now, if you don't think this behavior is usually surprising, and that most of the users would understand why $_
equals date
instead of hello
, then let's close this issue. But when I imagine myself working on a big script, and everything works fine, but suddenly I get weird bugs because I added a trap on debug somewhere in the code, I know I would have wanted to know about this behavior with $_
/traps, because debugging could be hard.
I hope it's a bit more clear.
Please note that I don't mind closing this. I just wanted to update the status on this so I could either send a PR or close the issue 🙂
Now for the workaround, that could be added as well if this behavior is described in a note or caveat:
trapfunc() {
local old_=$1 # remember the passed arg
date # trap contents, can be whatever
: "$old_" # store the remembered arg in $_ again
}
# each time the trap is executed,
# $_ will be passed to the function so it can restore it at the end
trap 'trapfunc "$_"' DEBUG
Note that using $*
or $@
would not be correct here. $_
is one argument only. We pass it to the function, it receives it in $1
, and stores it again in $_
at the end.
from pure-bash-bible.
I don't think I can write a better explanation for this. I'm going to close, but feel free to re-open!
from pure-bash-bible.
Related Issues (20)
- Alternative to dos2unix
- Inaccurate Caveat for `split()`
- Much More Compatible `reverse_array()`
- Splitting a string on a delimiter HOT 1
- Bash HOT 1
- Pedantic use of ':' "in place of a temporary variable." HOT 1
- shift at least... HOT 2
- basename with one argument fails with set -u HOT 1
- read, store, write a file including nulls
- Is this project active? HOT 4
- Typo in function date HOT 1
- Using coproc with read in non-interactive shells for sleep alternative
- Bash
- [[ ' aaaa' =~ a* ]] && echo ${BASH_REMATCH[0]} || echo no HOT 8
- for loop HOT 3
- wierd if statement HOT 1
- Is the Repository Dead? HOT 1
- Bash HOT 1
- bash read multi-line string with Process Substitution HOT 1
- UPPER CASE %02X instead of x
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pure-bash-bible.