Skip to content

Fix Issue #367: Value of $_ overwritten in bash #381

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.3.10 / TBA

#### auto.sh

* Preserve the value of `$_` for `bash` users. (@HaleTom)

### 0.3.9 / 2014-11-23

#### chruby.sh
Expand Down
4 changes: 3 additions & 1 deletion share/chruby/auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unset RUBY_AUTO_VERSION

function chruby_auto() {
local dir="$PWD/" version
local old_="$1" # Save $_ for bash users

until [[ -z "$dir" ]]; do
dir="${dir%/*}"
Expand All @@ -22,12 +23,13 @@ function chruby_auto() {
chruby_reset
unset RUBY_AUTO_VERSION
fi
: "$old_" # restore $_ (last argument of last command executed)
}

if [[ -n "$ZSH_VERSION" ]]; then
if [[ ! "$preexec_functions" == *chruby_auto* ]]; then
preexec_functions+=("chruby_auto")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe zsh also supports the $_ variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but it does not suffer from this issue.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be time to create a special chruby_auto_bash function to wrap the Bash-specific logic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the save/restore $_ also works in zsh, even though it is not required as zsh saves/restores automagically.

fi
elif [[ -n "$BASH_VERSION" ]]; then
trap '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chruby_auto' DEBUG
trap '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chruby_auto "$_"' DEBUG
fi
5 changes: 5 additions & 0 deletions test/unit/chruby_auto_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ function test_chruby_auto_ruby_version_containing_options()
"$expected_auto_version" "$RUBY_AUTO_VERSION"
}

function test_chruby_auto_preserves_dollar_underscore() {
: foo # Run a null command to set $_ to "foo"
assertEquals "value of $_ does not change" "$_" "foo"
}

function tearDown()
{
cd "$original_pwd"
Expand Down