From 3ee395f9b576e4111650cef67328f4ffe3687b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 13 Oct 2014 12:24:45 +0200 Subject: [PATCH] Clean up PATH sanitization in rbenv-which --- libexec/rbenv-which | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) 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