Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't clobber $_ #32

Merged
merged 6 commits into from
Feb 12, 2017
Merged

Don't clobber $_ #32

merged 6 commits into from
Feb 12, 2017

Conversation

HaleTom
Copy link
Contributor

@HaleTom HaleTom commented Dec 3, 2016

$_ should give the last argument of the last command.

Currently it gives __bp_preexec_invoke_exec:

$ exec bash
$ . ./bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ echo $_
just typed echo $_
__bp_preexec_invoke_exec
$

Fix this to produce the expected behaviour:

exec bash
$ . ./bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ echo $_
just typed echo $_
./bash-preexec.sh

Note that bash-bats currently suffers from the same bug (it's a feature of bash), so the newly added test fails. I have manually tested it and it passes.

@rcaloras
Copy link
Owner

rcaloras commented Dec 4, 2016

@HaleTom Thanks for the PR! I'm traveling at the moment, but I'll give this a look in a few days and would love to merge this in.

 - Include $_ on our trap invocation to preserve its value. It should simply
   forward the previous value so that it's now availble.

 - Previoulsy it just be set to __bp_preexec_invoke_exec.
@rcaloras
Copy link
Owner

rcaloras commented Dec 19, 2016

@HaleTom Just updated the PR. I think all it needs is to have $_ passed additionally in the trap function. That effectively forwards the value so it should be available afterwards. Setting it in __bp_preexec_invoke_exec via : "$old_" is unnecessary I believe. Tested and seemed fine for me locally.

Give it a try. If it looks good I'll merge 👍 Thanks for your help!

@HaleTom
Copy link
Contributor Author

HaleTom commented Dec 20, 2016

Not for me:

@ravi@genesis:~/repo/bash-preexec(fix-dollar_underscore)$ source bash-preexec.sh
@ravi@genesis:~/repo/bash-preexec(fix-dollar_underscore)$ echo $_
__bp_preexec_invoke_exec
@ravi@genesis:~/repo/bash-preexec(fix-dollar_underscore)$ echo 1 2 3
1 2 3
@ravi@genesis:~/repo/bash-preexec(fix-dollar_underscore)$ echo $_
__bp_preexec_invoke_exec
@ravi@genesis:~/repo/bash-preexec(fix-dollar_underscore)$ 

GNU bash, version 4.4.5(1)-release (x86_64-unknown-linux-gnu)

@rcaloras
Copy link
Owner

Hmm is that in the same session as your previous version of bash-preexec? bash-preexec won't override a previous version that's already included in the current bash session by checking __bp_imported. That could be causing the issue.

I just tried testing this PR on a slightly older version of bash 4.4 and I can't recreate :/

elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ source bash-preexec.sh 
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ echo $BASH_VERSION
4.4.0(1)-release
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ echo $_
4.4.0(1)-release
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ : 1 2 3
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ echo $_
3
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ preexec() { echo "Hello $1"; }
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ echo $_                       
Hello echo $_
3
elementz@Kashmir:~/git/HaleTom/bash-preexec (fix-dollar_underscore)$ 

Let me see if I can get a version of 4.4.5 to try as well.

@HaleTom
Copy link
Contributor Author

HaleTom commented Dec 21, 2016

I just double checked - no previous sourcing of bash-preexec.sh but still the same behaviour. chruby was also disabled.

I have:
PROMPT_COMMAND=__set_bash_prompt... I'm assuming that this shouldn't affect anything.

@rcaloras
Copy link
Owner

Hmm okay, I'll add back your preservation of $_ inside _bp_preexec_invoke_exec. Will push in a min.

@HaleTom
Copy link
Contributor Author

HaleTom commented Dec 21, 2016

What about the test... another PR when I get around to fixing bats?

@rcaloras
Copy link
Owner

Pushed that latest, @HaleTom give it a try locally.

The previous test case added was not correct. I removed it in my first commit. I don't see an easy way to test this since it depends on the internal workings of bash to update $_ and the debug trap. Bats is more for unit tests on individual functions. If this patchset works for you, we can give it some more thought regarding a potential test case.

@rcaloras
Copy link
Owner

Bump. @HaleTom any chance to review the latest?

@HaleTom
Copy link
Contributor Author

HaleTom commented Dec 31, 2016

I haven't forgotten about this - I'm just rebuilding a server and doing the usual Festivus activities.

It will likely be two weeks as I'm also moving through 2 countries in that time.

Happy New Year!

@rcaloras
Copy link
Owner

rcaloras commented Jan 3, 2017

@HaleTom no worries. Please give it a try when you get a moment.

If anyone else in the community has a sec, please give this a run to see if $_ is being preserved. It's working locally for me, but I want to test across a few other systems and bash versions.

@rcaloras
Copy link
Owner

@HaleTom Bump! Got a sec to test this PR?

@rcaloras
Copy link
Owner

@HaleTom I'm planning on merging this and cutting 3.2. Please check it against your setup when you're available.

@rcaloras rcaloras force-pushed the fix-dollar_underscore branch 4 times, most recently from 8698348 to 4e033ce Compare February 7, 2017 07:04
@rcaloras rcaloras force-pushed the fix-dollar_underscore branch from 4e033ce to d056f7d Compare February 7, 2017 17:25
@rcaloras
Copy link
Owner

👍 🍔 🍟

@rcaloras rcaloras merged commit 0c2f061 into rcaloras:master Feb 12, 2017
@rcaloras
Copy link
Owner

@HaleTom feel free to reopen as needed. Thanks for your contribution and opening this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants