From d3a35d3c3313cb87ad3089fbc8e90762563f6f7a Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 30 Oct 2017 16:19:44 -0400 Subject: [PATCH] aliases: cd: fix return code with basic failures --- .profile.d/aliases.sh | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/.profile.d/aliases.sh b/.profile.d/aliases.sh index accde15..dbbf4f4 100644 --- a/.profile.d/aliases.sh +++ b/.profile.d/aliases.sh @@ -102,27 +102,34 @@ cd_history() { ;; esac fi + if command cd "$@" ; then pushd -n "${PWD}" >/dev/null - elif [[ $# -gt 1 ]] ; then - # The `cd` above should have shown an error message for us. - local arg first_arg="$1" - while [[ $# -gt 0 ]] ; do - arg="$1" - shift - if [[ -d ${arg} ]] ; then - if [[ $# -gt 0 ]] ; then - printf 'cd: remaining: %s\n' "$@" >&2 + else + local ret=$? + + if [[ $# -gt 1 ]] ; then + # The `cd` above should have shown an error message for us. + local arg first_arg="$1" + while [[ $# -gt 0 ]] ; do + arg="$1" + shift + if [[ -d ${arg} ]] ; then + if [[ $# -gt 0 ]] ; then + printf 'cd: remaining: %s\n' "$@" >&2 + fi + printf 'cd: entering: %s\n' "${arg}" >&2 + cd_history "${arg}" + return + else + printf 'cd: skipping: %s\n' "${arg}" >&2 fi - printf 'cd: entering: %s\n' "${arg}" >&2 - cd_history "${arg}" - return - else - printf 'cd: skipping: %s\n' "${arg}" >&2 - fi - done - # If we're still here, then we didn't cd anywhere. - echo "cd: cwd is unchanged!" >&2 + done + # If we're still here, then we didn't cd anywhere. + echo "cd: cwd is unchanged!" >&2 + fi + + return ${ret} fi } alias cd='cd_history' -- 2.39.5