diff --git a/libexec/rbenv-which b/libexec/rbenv-which index 6b0d93a2..f656537d 100755 --- a/libexec/rbenv-which +++ b/libexec/rbenv-which @@ -15,6 +15,16 @@ if [ "$1" = "--complete" ]; then exec rbenv shims --short fi +remove_from_path() { + local path_to_remove="$1" + local path_before + local result=":$PATH:" + while [ "$path_before" != "$result" ]; do + path_before="$result" + result="${result//:$path_to_remove:/:}" + done + echo "${result%:}" +} RBENV_COMMAND="$1" @@ -26,18 +36,8 @@ fi RBENV_VERSION="${RBENV_VERSION:-$(rbenv-version-name)}" if [ "$RBENV_VERSION" = "system" ]; then - # Remove shims from PATH. Use a loop, because Bash won't remove all ":foo:" - # in ":foo:foo:" in one go. - path=":$PATH:" - remove="${RBENV_ROOT}/shims" - while true; do - path_before="$path" - path="${path//:$remove:/:}" - if [[ "$path_before" = "$path" ]]; then - break - fi - done - RBENV_COMMAND_PATH="$(PATH=$path command -v "$RBENV_COMMAND" || true)" + PATH="$(remove_from_path "${RBENV_ROOT}/shims")" + RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND" || true)" else RBENV_COMMAND_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}" fi